Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions llvm/test/tools/llvm-snippy/dump-initial-registers-no-model.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# RUN: llvm-snippy %s -mtriple=riscv64 --model-plugin=None -num-instrs=10 \
# RUN: --init-regs-in-elf \
# RUN: --dump-initial-registers-yaml=%t.initial-regs.yaml
# RUN: test -s %t.initial-regs.yaml

include:
- "Inputs/sections.yaml"

histogram:
- [ADD, 1]
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ class SnippyProgramContext final {
const IRegisterState &
getInitialRegisterState(const TargetSubtargetInfo &ST) const;

void dumpInitialRegisterState(StringRef YamlPath) const;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hence this overload is redundant

void dumpInitialRegisterState(StringRef YamlPath,
const TargetSubtargetInfo &ST) const;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also separate concerns here. Let's separate fetching this state and dumping it. So in FlowGenerator you can do:

auto &RegState = ProgramCtx.getInitialRegisterState(ST);
RegState.saveAsYAMLFile(PassCfg.RegistersConfig.InitialStateOutputYaml);


const IRegisterState &getInitialRegisterState() const;

bool hasProgramStateSaveSpace() const { return PGSK.get(); }
const auto &getProgramStateSaveSpace() const { return *PGSK; }
auto &getProgramStateSaveSpace() { return *PGSK; }
Expand Down Expand Up @@ -364,6 +370,8 @@ class SnippyProgramContext final {
std::unique_ptr<MemoryManager> MemManager;
std::unique_ptr<SMCManagerT> SMCManager;

const TargetSubtargetInfo *InitialStateSubtarget = nullptr;

constexpr static auto SmallStringDefaultSize = 16;

std::optional<SectionDesc> ROMSection;
Expand Down
3 changes: 3 additions & 0 deletions llvm/tools/llvm-snippy/lib/FlowGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ GeneratorResult FlowGenerator::generate(LLVMState &State,

} else {

ProgContext.dumpInitialRegisterState(
PassCfg.RegistersConfig.InitialStateOutputYaml);

Comment on lines +507 to +509

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There on the call site you can get the subtarget

auto *EntryFunc  = MainModule.getFunction(ProgramCfg.EntryPointName);
assert(EntryFunc);
auto &ST = State.getSubtargetImpl(*EntryFunc);
// Now you can call
dumpInitialRegisterState(PassCfg.RegistersConfig.InitialStateOutputYaml, ST);

snippy::warn(WarningName::NoModelExec, State.getCtx(),
"Skipping snippet execution on the model",
"model was set to 'None'.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ Error SimulatorContext::runSimulator(const RunInfo &RI) {
// StartPC location may be updated since last time it was configured.

I.setPC(ElfData->ProgStart);
I.dumpCurrentRegState(InitialStateOutputYaml);
ProgCtx.dumpInitialRegisterState(InitialStateOutputYaml, I.getSubTarget());

if (ElfData->ProgEnd == std::nullopt)
return createStringError(
Expand Down
33 changes: 33 additions & 0 deletions llvm/tools/llvm-snippy/lib/Generator/SnippyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,38 @@ const IRegisterState &SnippyProgramContext::getInitialRegisterState(
return *InitialMachineState;
}

void SnippyProgramContext::dumpInitialRegisterState(
StringRef YamlPath, const TargetSubtargetInfo &ST) const {
if (YamlPath.empty())
return;

std::error_code EC;
raw_fd_ostream File(YamlPath, EC);
if (EC)
snippy::fatal(State->getCtx(), "Failed to dump initial registers",
"cannot open file \"" + Twine(YamlPath) +
"\": " + EC.message());

getInitialRegisterState(ST).saveAsYAMLFile(File);
}

void SnippyProgramContext::dumpInitialRegisterState(StringRef YamlPath) const {
if (YamlPath.empty())
return;

assert(
InitialStateSubtarget &&
"Initial register state requested before target context initialization");
dumpInitialRegisterState(YamlPath, *InitialStateSubtarget);
}

const IRegisterState &SnippyProgramContext::getInitialRegisterState() const {
assert(InitialStateSubtarget &&
"Initial register state requested before target context "
"initialization");
return getInitialRegisterState(*InitialStateSubtarget);
}

SnippyProgramContext::SnippyProgramContext(LLVMState &State,
RegisterGenerator &RegGen,
std::vector<RegPool> Pools,
Expand Down Expand Up @@ -318,6 +350,7 @@ void SnippyProgramContext::createTargetContext(const Config &Cfg,
const TargetSubtargetInfo &STI,
const RegPoolWrapper &RP) {
assert(!TargetContext && "Double context insertion");
InitialStateSubtarget = &STI;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this new member and it's usage
We dump register state after we generated code so there're no problems with getting subtarget from LLVMState at this point. Just call getLLVMState().getSubtargetImpl

TargetContext =
State->getSnippyTarget().createTargetContext(*State, Cfg, &STI, RP);
}
Expand Down
Loading