|
40 | 40 | #include "llvm/IR/MDBuilder.h"
|
41 | 41 | #include "llvm/IR/Metadata.h"
|
42 | 42 | #include "llvm/IR/PassManager.h"
|
| 43 | +#include "llvm/IR/ReplaceConstant.h" |
43 | 44 | #include "llvm/IR/Value.h"
|
44 | 45 | #include "llvm/MC/TargetRegistry.h"
|
45 | 46 | #include "llvm/Support/CommandLine.h"
|
@@ -5098,27 +5099,6 @@ FunctionCallee OpenMPIRBuilder::createDispatchFiniFunction(unsigned IVSize,
|
5098 | 5099 | return getOrCreateRuntimeFunction(M, Name);
|
5099 | 5100 | }
|
5100 | 5101 |
|
5101 |
| -static void replaceConstatExprUsesInFuncWithInstr(ConstantExpr *ConstExpr, |
5102 |
| - Function *Func) { |
5103 |
| - for (User *User : make_early_inc_range(ConstExpr->users())) { |
5104 |
| - if (auto *Instr = dyn_cast<Instruction>(User)) { |
5105 |
| - if (Instr->getFunction() == Func) { |
5106 |
| - Instruction *ConstInst = ConstExpr->getAsInstruction(); |
5107 |
| - ConstInst->insertBefore(*Instr->getParent(), Instr->getIterator()); |
5108 |
| - Instr->replaceUsesOfWith(ConstExpr, ConstInst); |
5109 |
| - } |
5110 |
| - } |
5111 |
| - } |
5112 |
| -} |
5113 |
| - |
5114 |
| -static void replaceConstantValueUsesInFuncWithInstr(llvm::Value *Input, |
5115 |
| - Function *Func) { |
5116 |
| - for (User *User : make_early_inc_range(Input->users())) |
5117 |
| - if (auto *Const = dyn_cast<Constant>(User)) |
5118 |
| - if (auto *ConstExpr = dyn_cast<ConstantExpr>(Const)) |
5119 |
| - replaceConstatExprUsesInFuncWithInstr(ConstExpr, Func); |
5120 |
| -} |
5121 |
| - |
5122 | 5102 | static Function *createOutlinedFunction(
|
5123 | 5103 | OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder, StringRef FuncName,
|
5124 | 5104 | SmallVectorImpl<Value *> &Inputs,
|
@@ -5197,17 +5177,23 @@ static Function *createOutlinedFunction(
|
5197 | 5177 |
|
5198 | 5178 | // Things like GEP's can come in the form of Constants. Constants and
|
5199 | 5179 | // ConstantExpr's do not have access to the knowledge of what they're
|
5200 |
| - // contained in, so we must dig a little to find an instruction so we can |
5201 |
| - // tell if they're used inside of the function we're outlining. We also |
5202 |
| - // replace the original constant expression with a new instruction |
| 5180 | + // contained in, so we must dig a little to find an instruction so we |
| 5181 | + // can tell if they're used inside of the function we're outlining. We |
| 5182 | + // also replace the original constant expression with a new instruction |
5203 | 5183 | // equivalent; an instruction as it allows easy modification in the
|
5204 |
| - // following loop, as we can now know the constant (instruction) is owned by |
5205 |
| - // our target function and replaceUsesOfWith can now be invoked on it |
5206 |
| - // (cannot do this with constants it seems). A brand new one also allows us |
5207 |
| - // to be cautious as it is perhaps possible the old expression was used |
5208 |
| - // inside of the function but exists and is used externally (unlikely by the |
5209 |
| - // nature of a Constant, but still). |
5210 |
| - replaceConstantValueUsesInFuncWithInstr(Input, Func); |
| 5184 | + // following loop, as we can now know the constant (instruction) is |
| 5185 | + // owned by our target function and replaceUsesOfWith can now be invoked |
| 5186 | + // on it (cannot do this with constants it seems). A brand new one also |
| 5187 | + // allows us to be cautious as it is perhaps possible the old expression |
| 5188 | + // was used inside of the function but exists and is used externally |
| 5189 | + // (unlikely by the nature of a Constant, but still). |
| 5190 | + // NOTE: We cannot remove dead constants that have been rewritten to |
| 5191 | + // instructions at this stage, we run the risk of breaking later lowering |
| 5192 | + // by doing so as we could still be in the process of lowering the module |
| 5193 | + // from MLIR to LLVM-IR and the MLIR lowering may still require the original |
| 5194 | + // constants we have created rewritten versions of. |
| 5195 | + if (auto *Const = dyn_cast<Constant>(Input)) |
| 5196 | + convertUsersOfConstantsToInstructions(Const, Func, false); |
5211 | 5197 |
|
5212 | 5198 | // Collect all the instructions
|
5213 | 5199 | for (User *User : make_early_inc_range(Input->users()))
|
|
0 commit comments