Skip to content

Commit 44261da

Browse files
authoredMar 21, 2025··
[flang][NFC] use hlfir.declare first result when both results are raw pointers (#132261)
Currently, the helpers to get fir::ExtendedValue out of hlfir::Entity use hlfir.declare second result (`#1`) in most cases. This is because this result is the same as the input and matches what FIR was getting before lowering to HLFIR. But this creates odd situations when both hlfir.declare are raw pointers and either result ends-up being used in the IR depending on whether the code was generated by a helper using fir::ExtendedValue, or via "pure HLFIR" helpers using the first result. This will typically prevent simple CSE and easy identification that two operation (e.g load/store) are touching the exact same memory location without using alias analysis or "manual detection" (looking for common hlfir.declare defining op). Hence, when hlfir.declare results are both raw pointers, use `#0` when producing `fir::ExtendedValue`. When `#0` is a fir.box, keep using `#1` because these are not the same. The only code change is in HLFIRTools.cpp and is pretty small, but there is a big test fallout of `#1` to `#0`.
1 parent 387f3e8 commit 44261da

File tree

214 files changed

+1039
-1027
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

214 files changed

+1039
-1027
lines changed
 

Diff for: ‎flang/lib/Optimizer/Builder/HLFIRTools.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,24 @@ static fir::CharBoxValue genUnboxChar(mlir::Location loc,
186186
return {addr, len};
187187
}
188188

189+
// To maximize chances of identifying usage of a same variables in the IR,
190+
// always return the hlfirBase result of declare/associate if it is a raw
191+
// pointer.
192+
static mlir::Value getFirBaseHelper(mlir::Value hlfirBase,
193+
mlir::Value firBase) {
194+
if (fir::isa_ref_type(hlfirBase.getType()))
195+
return hlfirBase;
196+
return firBase;
197+
}
198+
189199
mlir::Value hlfir::Entity::getFirBase() const {
190200
if (fir::FortranVariableOpInterface variable = getIfVariableInterface()) {
191201
if (auto declareOp =
192202
mlir::dyn_cast<hlfir::DeclareOp>(variable.getOperation()))
193-
return declareOp.getOriginalBase();
203+
return getFirBaseHelper(declareOp.getBase(), declareOp.getOriginalBase());
194204
if (auto associateOp =
195205
mlir::dyn_cast<hlfir::AssociateOp>(variable.getOperation()))
196-
return associateOp.getFirBase();
206+
return getFirBaseHelper(associateOp.getBase(), associateOp.getFirBase());
197207
}
198208
return getBase();
199209
}

Diff for: ‎flang/test/HLFIR/all-lowering.fir

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func.func @_QPall3(%arg0: !fir.ref<!fir.array<2x!fir.logical<4>>> {fir.bindc_nam
9090

9191
// CHECK-DAG: %[[MASK_ADDR:.*]] = fir.address_of
9292
// CHECK-DAG: %[[MASK_VAR:.*]]:2 = hlfir.declare %[[MASK_ADDR]](%[[MASK_SHAPE:.*]])
93-
// CHECK-DAG: %[[MASK_BOX:.*]] = fir.embox %[[MASK_VAR]]#1(%[[MASK_SHAPE:.*]])
93+
// CHECK-DAG: %[[MASK_BOX:.*]] = fir.embox %[[MASK_VAR]]#0(%[[MASK_SHAPE:.*]])
9494

9595
// CHECK-DAG: %[[DIM:.*]] = arith.constant 1 : i32
9696

0 commit comments

Comments
 (0)