Skip to content

Commit ce8022f

Browse files
committed
[flang] Upstream partial lowering of EXIT intrinsic
This patch adds partial lowering of the "EXIT" intrinsic to the backend runtime hook implemented in patch D110741. It also adds a helper function to the `RuntimeCallTestBase.h` for testing for an intrinsic function call in a `mlir::Block`. Differential Revision: https://reviews.llvm.org/D118141
1 parent 7518d38 commit ce8022f

File tree

6 files changed

+91
-0
lines changed

6 files changed

+91
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- Stop.h - generate stop runtime API calls ----------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef FORTRAN_OPTIMIZER_BUILDER_RUNTIME_STOP_H
10+
#define FORTRAN_OPTIMIZER_BUILDER_RUNTIME_STOP_H
11+
12+
namespace mlir {
13+
class Value;
14+
class Location;
15+
} // namespace mlir
16+
17+
namespace fir {
18+
class FirOpBuilder;
19+
}
20+
21+
namespace fir::runtime {
22+
23+
/// Generate call to EXIT intrinsic runtime routine.
24+
void genExit(fir::FirOpBuilder &, mlir::Location, mlir::Value status);
25+
26+
} // namespace fir::runtime
27+
#endif // FORTRAN_OPTIMIZER_BUILDER_RUNTIME_STOP_H

flang/lib/Optimizer/Builder/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_flang_library(FIRBuilder
1414
Runtime/Numeric.cpp
1515
Runtime/Ragged.cpp
1616
Runtime/Reduction.cpp
17+
Runtime/Stop.cpp
1718
Runtime/Transformational.cpp
1819

1920
DEPENDS
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Stop.h - generate stop runtime API calls ----------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "flang/Optimizer/Builder/Runtime/Stop.h"
10+
#include "flang/Optimizer/Builder/FIRBuilder.h"
11+
#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
12+
#include "flang/Runtime/stop.h"
13+
14+
using namespace Fortran::runtime;
15+
16+
void fir::runtime::genExit(fir::FirOpBuilder &builder, mlir::Location loc,
17+
mlir::Value status) {
18+
auto exitFunc = fir::runtime::getRuntimeFunc<mkRTKey(Exit)>(loc, builder);
19+
llvm::SmallVector<mlir::Value> args =
20+
fir::runtime::createArguments(builder, loc, exitFunc.getType(), status);
21+
builder.create<fir::CallOp>(loc, exitFunc, args);
22+
}

flang/unittests/Optimizer/Builder/Runtime/RuntimeCallTestBase.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,24 @@ static inline void checkCallOpFromResultBox(mlir::Value result,
120120
checkCallOpFromResultBox(convOp.getResult(), fctName, nbArgs, addLocArgs);
121121
}
122122

123+
/// Check the operations in \p block for a `fir::CallOp` operation where the
124+
/// function being called shares its function name with \p fctName and the
125+
/// number of arguments is equal to \p nbArgs. Note that this check only cares
126+
/// if the operation exists, and not the order in when the operation is called.
127+
/// This results in exiting the test as soon as the first correct instance of
128+
/// `fir::CallOp` is found).
129+
static inline void checkBlockForCallOp(
130+
mlir::Block *block, llvm::StringRef fctName, unsigned nbArgs) {
131+
assert(block && "mlir::Block given is a nullptr");
132+
for (auto &op : block->getOperations()) {
133+
if (auto callOp = mlir::dyn_cast<fir::CallOp>(op)) {
134+
if (fctName == callOp.callee()->getRootReference().getValue()) {
135+
EXPECT_EQ(nbArgs, callOp.args().size());
136+
return;
137+
}
138+
}
139+
}
140+
FAIL() << "No calls to " << fctName << " were found!";
141+
}
142+
123143
#endif // FORTRAN_OPTIMIZER_BUILDER_RUNTIME_RUNTIMECALLTESTBASE_H
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===- StopTest.cpp -- Stop runtime builder unit tests ---------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "flang/Optimizer/Builder/Runtime/Stop.h"
10+
#include "RuntimeCallTestBase.h"
11+
#include "gtest/gtest.h"
12+
13+
TEST_F(RuntimeCallTest, genExitTest) {
14+
mlir::Location loc = firBuilder->getUnknownLoc();
15+
mlir::Value status = firBuilder->createIntegerConstant(loc, i32Ty, 0);
16+
fir::runtime::genExit(*firBuilder, loc, status);
17+
mlir::Block *block = firBuilder->getBlock();
18+
EXPECT_TRUE(block) << "Failed to retrieve the block!";
19+
checkBlockForCallOp(block, "_FortranAExit", 1);
20+
}

flang/unittests/Optimizer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_flang_unittest(FlangOptimizerTests
2020
Builder/Runtime/NumericTest.cpp
2121
Builder/Runtime/RaggedTest.cpp
2222
Builder/Runtime/ReductionTest.cpp
23+
Builder/Runtime/StopTest.cpp
2324
Builder/Runtime/TransformationalTest.cpp
2425
FIRContextTest.cpp
2526
InternalNamesTest.cpp

0 commit comments

Comments
 (0)