summaryrefslogtreecommitdiff
path: root/flang/lib/Optimizer/Transforms/GenRuntimeCallsForTest.cpp
diff options
context:
space:
mode:
authorSlava Zakharin <szakharin@nvidia.com>2025-02-24 09:27:48 -0800
committerGitHub <noreply@github.com>2025-02-24 09:27:48 -0800
commit36fdeb2aded08a776fcffefa73cb7667e7fc6c2d (patch)
treeaf6de1cf6d39d94453b6b562e18752e70a94e77b /flang/lib/Optimizer/Transforms/GenRuntimeCallsForTest.cpp
parentcc7f22ee6ccb2c1ad79834e06d5b18d8f014d140 (diff)
[flang] Set LLVM specific attributes to fir.call's of Fortran runtime. (#128093)
This change is inspired by a case in facerec benchmark, where performance of scalar code may improve by about 6%@aarch64 due to getting rid of redundant loads from Fortran descriptors. These descriptors are corresponding to subroutine local ALLOCATABLE, SAVE variables. The scalar loop nest in LocalMove subroutine contains call to Fortran runtime IO functions, and LLVM globals-aa analysis cannot prove that these calls do not modify the globalized descriptors with internal linkage. This patch sets and propagates llvm.memory_effects attribute for fir.call operations calling Fortran runtime functions. In particular, it tries to set the Other memory effect to NoModRef. The Other memory effect includes accesses to globals and captured pointers, so we cannot set it for functions taking Fortran descriptors with one exception for calls where the Fortran descriptor arguments are all null. As long as different calls to the same Fortran runtime function may have different attributes, I decided to attach the attributes to the calls rather than functions. Moreover, attaching the attributes to func.func will require propagating these attributes to llvm.func, which is not happening right now. In addition to llvm.memory_effects, the new pass sets llvm.nosync and llvm.nocallback attributes that may also help LLVM alias analysis (e.g. see #127707). These attributes are ignored currently. I will support them in LLVM IR dialect in a separate patch. I also added another pass for developers to be able to print declarations/calls of all Fortran runtime functions that are recognized by the attributes setting pass. It should help with maintenance of the LIT tests.
Diffstat (limited to 'flang/lib/Optimizer/Transforms/GenRuntimeCallsForTest.cpp')
-rw-r--r--flang/lib/Optimizer/Transforms/GenRuntimeCallsForTest.cpp106
1 files changed, 106 insertions, 0 deletions
diff --git a/flang/lib/Optimizer/Transforms/GenRuntimeCallsForTest.cpp b/flang/lib/Optimizer/Transforms/GenRuntimeCallsForTest.cpp
new file mode 100644
index 000000000000..8d1d62e334d8
--- /dev/null
+++ b/flang/lib/Optimizer/Transforms/GenRuntimeCallsForTest.cpp
@@ -0,0 +1,106 @@
+//===- GenRuntimeCallsForTest.cpp -----------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+//===----------------------------------------------------------------------===//
+/// \file
+/// This pass is only for developers to generate declarations/calls
+/// of Fortran runtime function recognized in
+/// flang/Optimizer/Transforms/RuntimeFunctions.inc table.
+/// Sample of the generated FIR:
+/// func.func private
+/// @_FortranAioSetStatus(!fir.ref<i8>, !fir.ref<i8>, i64) ->
+/// i1 attributes {fir.io, fir.runtime}
+///
+/// func.func @test__FortranAioSetStatus(
+/// %arg0: !fir.ref<i8>, %arg1: !fir.ref<i8>, %arg2: i64) -> i1 {
+/// %0 = fir.call @_FortranAioSetStatus(%arg0, %arg1, %arg2) :
+/// (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
+/// return %0 : i1
+/// }
+//===----------------------------------------------------------------------===//
+#include "flang/Common/static-multimap-view.h"
+#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
+#include "flang/Optimizer/Dialect/FIRDialect.h"
+#include "flang/Optimizer/Dialect/FIROpsSupport.h"
+#include "flang/Optimizer/Support/InternalNames.h"
+#include "flang/Optimizer/Transforms/Passes.h"
+#include "flang/Runtime/io-api-consts.h"
+#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
+
+namespace fir {
+#define GEN_PASS_DEF_GENRUNTIMECALLSFORTEST
+#include "flang/Optimizer/Transforms/Passes.h.inc"
+} // namespace fir
+
+#define DEBUG_TYPE "gen-runtime-calls-for-test"
+
+using namespace Fortran::runtime;
+using namespace Fortran::runtime::io;
+
+#define mkIOKey(X) FirmkKey(IONAME(X))
+#define mkRTKey(X) FirmkKey(RTNAME(X))
+
+namespace {
+class GenRuntimeCallsForTestPass
+ : public fir::impl::GenRuntimeCallsForTestBase<GenRuntimeCallsForTestPass> {
+ using GenRuntimeCallsForTestBase<
+ GenRuntimeCallsForTestPass>::GenRuntimeCallsForTestBase;
+
+public:
+ void runOnOperation() override;
+};
+} // end anonymous namespace
+
+static constexpr llvm::StringRef testPrefix = "test_";
+
+void GenRuntimeCallsForTestPass::runOnOperation() {
+ mlir::ModuleOp moduleOp = getOperation();
+ mlir::OpBuilder mlirBuilder(moduleOp.getRegion());
+ fir::FirOpBuilder builder(mlirBuilder, moduleOp);
+ mlir::Location loc = mlir::UnknownLoc::get(builder.getContext());
+
+#define KNOWN_IO_FUNC(X) \
+ fir::runtime::getIORuntimeFunc<mkIOKey(X)>(loc, builder)
+#define KNOWN_RUNTIME_FUNC(X) \
+ fir::runtime::getRuntimeFunc<mkRTKey(X)>(loc, builder)
+
+ mlir::func::FuncOp runtimeFuncsTable[] = {
+#include "flang/Optimizer/Transforms/RuntimeFunctions.inc"
+ };
+
+ if (!doGenerateCalls)
+ return;
+
+ // Generate thin wrapper functions calling the known Fortran
+ // runtime functions.
+ llvm::SmallVector<mlir::Operation *> newFuncs;
+ for (unsigned i = 0;
+ i < sizeof(runtimeFuncsTable) / sizeof(runtimeFuncsTable[0]); ++i) {
+ mlir::func::FuncOp funcOp = runtimeFuncsTable[i];
+ mlir::FunctionType funcTy = funcOp.getFunctionType();
+ std::string name = (llvm::Twine(testPrefix) + funcOp.getName()).str();
+ mlir::func::FuncOp callerFunc = builder.createFunction(loc, name, funcTy);
+ callerFunc.setVisibility(mlir::SymbolTable::Visibility::Public);
+ mlir::OpBuilder::InsertPoint insertPt = builder.saveInsertionPoint();
+
+ // Generate the wrapper function body that consists of a call and return.
+ builder.setInsertionPointToStart(callerFunc.addEntryBlock());
+ mlir::Block::BlockArgListType args = callerFunc.front().getArguments();
+ auto callOp = builder.create<fir::CallOp>(loc, funcOp, args);
+ builder.create<mlir::func::ReturnOp>(loc, callOp.getResults());
+
+ newFuncs.push_back(callerFunc.getOperation());
+ builder.restoreInsertionPoint(insertPt);
+ }
+
+ // Make sure all wrapper functions are at the beginning
+ // of the module.
+ auto moduleBegin = moduleOp.getBody()->begin();
+ for (auto func : newFuncs)
+ func->moveBefore(moduleOp.getBody(), moduleBegin);
+}