-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[CodeGen][NewPM] Handle --regalloc-npm
option
#94748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -409,13 +409,33 @@ class RequireAllMachineFunctionPropertiesPass | |
|
||
} // namespace | ||
|
||
static void defaultRegAllocBuilder(TargetMachine *TM, | ||
StringMap<MachineFunctionPassManager> &M) { | ||
if (!TM) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should always be non-null here |
||
return; | ||
|
||
MachineFunctionPassManager MFPM; | ||
auto Opts = getCGPassBuilderOption(); | ||
if (Opts.OptimizeRegAlloc.value_or(TM->getOptLevel() != | ||
CodeGenOptLevel::None)) { | ||
// TODO: Add greedy register allocator. | ||
} else { | ||
MFPM.addPass(RegAllocFastPass()); | ||
} | ||
M["all"] = std::move(MFPM); | ||
} | ||
|
||
PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO, | ||
std::optional<PGOOptions> PGOOpt, | ||
PassInstrumentationCallbacks *PIC) | ||
: TM(TM), PTO(PTO), PGOOpt(PGOOpt), PIC(PIC) { | ||
bool ShouldPopulateClassToPassNames = PIC && shouldPopulateClassToPassNames(); | ||
if (TM) | ||
if (TM) { | ||
DefaultRegAllocBuilder = [TM](StringMap<MachineFunctionPassManager> &M) { | ||
defaultRegAllocBuilder(TM, M); | ||
}; | ||
TM->registerPassBuilderCallbacks(*this, ShouldPopulateClassToPassNames); | ||
} | ||
if (ShouldPopulateClassToPassNames) { | ||
#define MODULE_PASS(NAME, CREATE_PASS) \ | ||
PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); | ||
|
@@ -2219,6 +2239,19 @@ Error PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) { | |
return Error::success(); | ||
} | ||
|
||
static StringRef getFilterName(StringRef PassName) { | ||
StringRef Params = PassName.drop_until([](char C) { return C == '<'; }); | ||
if (!Params.empty()) | ||
Params = Params.drop_front().drop_back(); | ||
while (!Params.empty()) { | ||
StringRef ParamName; | ||
std::tie(ParamName, Params) = Params.split(';'); | ||
if (ParamName.consume_front("filter=")) | ||
return ParamName; | ||
} | ||
return "all"; | ||
} | ||
|
||
RegClassFilterFunc PassBuilder::parseRegAllocFilter(StringRef FilterName) { | ||
if (FilterName == "all") | ||
return allocateAllRegClasses; | ||
|
@@ -2228,6 +2261,45 @@ RegClassFilterFunc PassBuilder::parseRegAllocFilter(StringRef FilterName) { | |
return nullptr; | ||
} | ||
|
||
Error PassBuilder::parseRegAllocOpt(StringRef Text) { | ||
assert(TM && "Need target machine to parse this option!"); | ||
if (RegAllocMap.empty()) | ||
DefaultRegAllocBuilder(RegAllocMap); | ||
|
||
MachineFunctionPassManager MFPM; | ||
if (Text == "default") { | ||
// Add nothing when target inserts "none" into the map. | ||
if (RegAllocMap.contains("none")) | ||
RegAllocMap["all"] = MachineFunctionPassManager(); | ||
return Error::success(); | ||
} | ||
|
||
if (RegAllocMap.contains("none")) { | ||
return make_error<StringError>( | ||
"Target doesn't support register allocation!", | ||
inconvertibleErrorCode()); | ||
} | ||
|
||
bool IsOptimized = TM->getOptLevel() != CodeGenOptLevel::None; | ||
while (!Text.empty()) { | ||
StringRef PassName; | ||
std::tie(PassName, Text) = Text.split(','); | ||
if (!IsOptimized && | ||
!PassBuilder::checkParametrizedPassName(PassName, "regallocfast")) { | ||
return make_error<StringError>( | ||
"Must use fast (default) register allocator for " | ||
"unoptimized regalloc.", | ||
inconvertibleErrorCode()); | ||
} | ||
// FIXME: Should only accept reg-alloc passes. | ||
if (auto Err = parsePassPipeline(MFPM, PassName)) | ||
return Err; | ||
RegAllocMap[getFilterName(PassName)] = std::move(MFPM); | ||
MFPM = MachineFunctionPassManager(); | ||
} | ||
return Error::success(); | ||
} | ||
|
||
static void printPassName(StringRef PassName, raw_ostream &OS) { | ||
OS << " " << PassName << "\n"; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems very roundabout to use a map that creates a MachineFunctionPassManager. can this instead be a callback that takes a MachineFunctionPassManager and adds a regalloc pass to it? like the optimization pipeline callbacks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The requirements for register allocation passes from backends are somewhat complex. Some targets (DirectX, NVPTX etc) shall not contain them. AMDGPU needs register allocators with correct parameters if pass supports it, here is the register filter. Callbacks may not suitable for this situation, callbacks may add some unexpected passes. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you mean callbacks may add unexpected passes?
basically my suggestion is replace
std::function<void(StringMap<MachineFunctionPassManager> &)>
withstd::function<void(MachineFunctionPassManager &, StringRef, bool)>
, whereStringRef
is the filter andbool
is optimized or not (or maybe that's part of filter? structure however makes most sense). the default callback adds Fast/GreedyRegAlloc based onIsOptimized
bool. AMDGPU can set the callback to add its custom regallocs based on the filter, and backends that don't need a regalloc can override the callback to be emptydoes that make sense? or is there a benefit of the StringMap over that approach?