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