[flang-commits] [flang] [mlir] [OpenMP] [MLIR] [Flang] Replace all uses of variables in ALLOCATE directive to use new value which is created. (PR #212361)
Sairudra More via flang-commits
flang-commits at lists.llvm.org
Wed Jul 29 19:59:41 PDT 2026
================
@@ -2437,6 +2439,60 @@ SmallVector<llvm::Value *> ModuleTranslation::lookupValues(ValueRange values) {
return remapped;
}
+static void remapConstantPointerUses(
+ llvm::Constant *oldPtr, llvm::Value *newPtr, llvm::IRBuilderBase &builder,
+ llvm::DenseMap<llvm::Constant *, llvm::Value *> &replacements) {
+ for (llvm::Use &use : llvm::make_early_inc_range(oldPtr->uses())) {
+ if (auto *constantExpr =
+ llvm::dyn_cast<llvm::ConstantExpr>(use.getUser())) {
+ if (constantExpr->getOpcode() == llvm::Instruction::GetElementPtr) {
+ auto *gep = llvm::cast<llvm::GEPOperator>(constantExpr);
+ llvm::SmallVector<llvm::Value *, 4> indices;
+ for (unsigned i = 1, e = constantExpr->getNumOperands(); i < e; ++i)
+ indices.push_back(constantExpr->getOperand(i));
+ llvm::Value *newGEP =
+ builder.CreateGEP(gep->getSourceElementType(), newPtr, indices);
+ replacements[constantExpr] = newGEP;
+ constantExpr->replaceAllUsesWith(newGEP);
+ continue;
+ }
+ llvm::Instruction *newInst = constantExpr->getAsInstruction();
+ builder.Insert(newInst);
+ replacements[constantExpr] = newInst;
+ constantExpr->replaceAllUsesWith(newInst);
+ continue;
+ }
+ use.set(newPtr);
+ }
+}
+
+void ModuleTranslation::remapAllValuesWith(llvm::Value *oldValue,
+ llvm::Value *newValue,
+ llvm::IRBuilderBase *builder) {
+ if (oldValue == newValue)
+ return;
+
+ llvm::DenseMap<llvm::Constant *, llvm::Value *> constantReplacements;
+ if (auto *constant = llvm::dyn_cast<llvm::Constant>(oldValue)) {
+ assert(builder &&
+ "IRBuilder required when remapping constant storage pointers");
+ remapConstantPointerUses(constant, newValue, *builder,
+ constantReplacements);
+ } else {
+ oldValue->replaceAllUsesWith(newValue);
----------------
Saieiei wrote:
`replaceAllUsesWith` also rewrites LLVM users emitted before `omp.allocate_dir`. In a verifier-valid use-before-directive case, this makes an earlier use reference the newly allocated value defined later, producing non-dominating IR. Could this update only the mapping for subsequent translations and add a regression test?
https://github.com/llvm/llvm-project/pull/212361
More information about the flang-commits
mailing list