Skip to content

Commit fe4d502

Browse files
authored
[flang] fix unsafe memory access using mlir::ValueRange (#78435)
When running the `flang/test/HLFIR/simplify-hlfir-intrinsics.fir` test case on AIX we encounter issues building op as they are not found in the mlir context: ``` LLVM ERROR: Building op `arith.subi` but it isn't known in this MLIRContext: the dialect may not be loaded or this operation hasn't been added by the dialect. See also https://mlir.llvm.org/getting_started/Faq/#registered-loaded-dependent-whats-up-with-dialects-management LLVM ERROR: Building op `hlfir.yield_element` but it isn't known in this MLIRContext: the dialect may not be loaded or this operation hasn't been added by the dialect. See also https://mlir.llvm.org/getting_started/Faq/#registered-loaded-dependent-whats-up-with-dialects-management LLVM ERROR: Building op `hlfir.yield_element` but it isn't known in this MLIRContext: the dialect may not be loaded or this operation hasn't been added by the dialect. See also https://mlir.llvm.org/getting_started/Faq/#registered-loaded-dependent-whats-up-with-dialects-management ``` The issue is caused by the "Merge disjoint stack slots" pass and the error is not present if the source is built with `-mllvm --no-stack-coloring` Thanks to investigation by @stefanp-ibm we found that "the initializer_list {inputIndices[1], inputIndices[0]} has a lifetime that only exists for the range of the constructor for ValueRange. Once we get to stack coloring we merge the stack slot for that element with another stack slot and then it gets overwritten which corrupts transposedIndices" The changes below prevents the corruption of transposedIndices and passes the test case. Co-authored-by: Mark Danial <[email protected]>
1 parent 296a684 commit fe4d502

File tree

2 files changed

+3
-4
lines changed

2 files changed

+3
-4
lines changed

flang/lib/Optimizer/HLFIR/Transforms/SimplifyHLFIRIntrinsics.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ class TransposeAsElementalConversion
5050
auto genKernel = [&array](mlir::Location loc, fir::FirOpBuilder &builder,
5151
mlir::ValueRange inputIndices) -> hlfir::Entity {
5252
assert(inputIndices.size() == 2 && "checked in TransposeOp::validate");
53-
mlir::ValueRange transposedIndices{{inputIndices[1], inputIndices[0]}};
53+
const std::initializer_list<mlir::Value> initList = {inputIndices[1],
54+
inputIndices[0]};
55+
mlir::ValueRange transposedIndices(initList);
5456
hlfir::Entity element =
5557
hlfir::getElementAt(loc, builder, array, transposedIndices);
5658
hlfir::Entity val = hlfir::loadTrivialScalar(loc, builder, element);

flang/test/HLFIR/simplify-hlfir-intrinsics.fir

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// XFail the following test case on AIX due to potential miscompilation
2-
// TODO: Crash fir-opt on AIX
3-
// XFAIL: system-aix
41
// RUN: fir-opt --simplify-hlfir-intrinsics %s | FileCheck %s
52

63
// box with known extents

0 commit comments

Comments
 (0)