summaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/CodeExtractor.cpp
diff options
context:
space:
mode:
authorTom Eccles <tom.eccles@arm.com>2024-11-04 11:20:32 +0000
committerGitHub <noreply@github.com>2024-11-04 11:20:32 +0000
commit4aaa92578686176243a294eeb2ca5697a99edcaa (patch)
tree05d633991e7b53859e622e6d85196a8e7e2fa9a8 /llvm/lib/Transforms/Utils/CodeExtractor.cpp
parente28d7f713471cf33908a0fe5223f480dfd9b06f0 (diff)
[llvm][CodeExtractor] fix bug in parameter naming (#114237)
The code extractor tries to apply the names of source input and output values to function arguments. Not all input and output values get added as arguments: some are instead placed inside of a struct passed to the function. The existing renaming code skipped trying to set these struct-packed arguments names (as there is no corresponding function argument to rename), but it still incremented the iterator over the function arguments. This could result in dereferencing an end iterator if struct-packed inputs/outputs preceded non-struct-packed inputs/outputs. This patch rewrites this loop to avoid the end iterator dereference.
Diffstat (limited to 'llvm/lib/Transforms/Utils/CodeExtractor.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/CodeExtractor.cpp24
1 files changed, 13 insertions, 11 deletions
diff --git a/llvm/lib/Transforms/Utils/CodeExtractor.cpp b/llvm/lib/Transforms/Utils/CodeExtractor.cpp
index ed4ad15e5ab6..fa467cc72bd0 100644
--- a/llvm/lib/Transforms/Utils/CodeExtractor.cpp
+++ b/llvm/lib/Transforms/Utils/CodeExtractor.cpp
@@ -823,17 +823,22 @@ Function *CodeExtractor::constructFunction(const ValueSet &inputs,
std::vector<Type *> ParamTy;
std::vector<Type *> AggParamTy;
+ std::vector<std::tuple<unsigned, Value *>> NumberedInputs;
+ std::vector<std::tuple<unsigned, Value *>> NumberedOutputs;
ValueSet StructValues;
const DataLayout &DL = M->getDataLayout();
// Add the types of the input values to the function's argument list
+ unsigned ArgNum = 0;
for (Value *value : inputs) {
LLVM_DEBUG(dbgs() << "value used in func: " << *value << "\n");
if (AggregateArgs && !ExcludeArgsFromAggregate.contains(value)) {
AggParamTy.push_back(value->getType());
StructValues.insert(value);
- } else
+ } else {
ParamTy.push_back(value->getType());
+ NumberedInputs.emplace_back(ArgNum++, value);
+ }
}
// Add the types of the output values to the function's argument list.
@@ -842,9 +847,11 @@ Function *CodeExtractor::constructFunction(const ValueSet &inputs,
if (AggregateArgs && !ExcludeArgsFromAggregate.contains(output)) {
AggParamTy.push_back(output->getType());
StructValues.insert(output);
- } else
+ } else {
ParamTy.push_back(
PointerType::get(output->getType(), DL.getAllocaAddrSpace()));
+ NumberedOutputs.emplace_back(ArgNum++, output);
+ }
}
assert(
@@ -1053,15 +1060,10 @@ Function *CodeExtractor::constructFunction(const ValueSet &inputs,
}
// Set names for input and output arguments.
- if (NumScalarParams) {
- ScalarAI = newFunction->arg_begin();
- for (unsigned i = 0, e = inputs.size(); i != e; ++i, ++ScalarAI)
- if (!StructValues.contains(inputs[i]))
- ScalarAI->setName(inputs[i]->getName());
- for (unsigned i = 0, e = outputs.size(); i != e; ++i, ++ScalarAI)
- if (!StructValues.contains(outputs[i]))
- ScalarAI->setName(outputs[i]->getName() + ".out");
- }
+ for (auto [i, argVal] : NumberedInputs)
+ newFunction->getArg(i)->setName(argVal->getName());
+ for (auto [i, argVal] : NumberedOutputs)
+ newFunction->getArg(i)->setName(argVal->getName() + ".out");
// Rewrite branches to basic blocks outside of the loop to new dummy blocks
// within the new function. This must be done before we lose track of which