[flang-commits] [flang] [mlir] [OpenMP] [MLIR] [Flang] Replace all uses of variables in ALLOCATE directive to use new value which is created. (PR #212361)
Sunil Shrestha via flang-commits
flang-commits at lists.llvm.org
Mon Aug 10 23:57:34 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,
----------------
sshrestha-aa wrote:
remapConstantPointerUses runs only when the variable's storage base is a constant pointer. For a Fortran variable, a constant-address base implies static storage duration (global). Since those are TODOs and excluded at lowering, is this path still reachable? If not, could this be dropped until global support lands?
https://github.com/llvm/llvm-project/pull/212361
More information about the flang-commits
mailing list