[flang-commits] [flang] [flang] Implement !DIR$ [NO]INLINE and FORCEINLINE directives (PR #134350)
Jean-Didier PAILLEUX via flang-commits
flang-commits at lists.llvm.org
Mon Oct 27 02:11:36 PDT 2025
https://github.com/JDPailleux updated https://github.com/llvm/llvm-project/pull/134350
>From f075262047cbf5bf7cb3724f808c3986fb2ef411 Mon Sep 17 00:00:00 2001
From: Jean-Didier Pailleux <jean-didier.pailleux at sipearl.com>
Date: Fri, 4 Apr 2025 08:55:20 +0200
Subject: [PATCH 1/3] [flang] Implement !DIR$ NOINLINE, INLINE and FORCEINLINE
directives
---
flang/include/flang/Evaluate/call.h | 10 ++
.../flang/Optimizer/Dialect/FIRAttr.td | 20 +++
.../include/flang/Optimizer/Dialect/FIROps.td | 1 +
flang/include/flang/Parser/dump-parse-tree.h | 3 +
flang/include/flang/Parser/parse-tree.h | 8 +-
flang/lib/Lower/Bridge.cpp | 128 +++++++++++++++++-
flang/lib/Lower/ConvertCall.cpp | 13 +-
flang/lib/Optimizer/CodeGen/CodeGen.cpp | 12 ++
.../Transforms/PolymorphicOpConversion.cpp | 3 +-
flang/lib/Parser/Fortran-parsers.cpp | 8 ++
flang/lib/Parser/unparse.cpp | 7 +
.../lib/Semantics/canonicalize-directives.cpp | 6 +-
flang/lib/Semantics/resolve-names.cpp | 5 +-
flang/test/Integration/inline_directive.f90 | 69 ++++++++++
flang/test/Lower/inline_directive.f90 | 61 +++++++++
flang/test/Parser/compiler-directives.f90 | 24 ++++
16 files changed, 367 insertions(+), 11 deletions(-)
create mode 100644 flang/test/Integration/inline_directive.f90
create mode 100644 flang/test/Lower/inline_directive.f90
diff --git a/flang/include/flang/Evaluate/call.h b/flang/include/flang/Evaluate/call.h
index 2a5929b873d74..b09f203c0de37 100644
--- a/flang/include/flang/Evaluate/call.h
+++ b/flang/include/flang/Evaluate/call.h
@@ -254,6 +254,13 @@ class ProcedureRef {
bool IsElemental() const { return proc_.IsElemental(); }
bool hasAlternateReturns() const { return hasAlternateReturns_; }
+ bool hasNoInline() const { return noInline_; }
+ void set_noInline(bool ni) { noInline_ = ni; }
+ bool hasAlwaysInline() const { return alwaysInline_; }
+ void set_alwaysInline(bool ai) { alwaysInline_ = ai; }
+ bool hasInlineHint() const { return inlineHint_; }
+ void set_inlineHint(bool ih) { inlineHint_ = ih; }
+
Expr<SomeType> *UnwrapArgExpr(int n) {
if (static_cast<std::size_t>(n) < arguments_.size() && arguments_[n]) {
return arguments_[n]->UnwrapExpr();
@@ -277,6 +284,9 @@ class ProcedureRef {
ActualArguments arguments_;
Chevrons chevrons_;
bool hasAlternateReturns_;
+ bool noInline_{false};
+ bool alwaysInline_{false};
+ bool inlineHint_{false};
};
template <typename A> class FunctionRef : public ProcedureRef {
diff --git a/flang/include/flang/Optimizer/Dialect/FIRAttr.td b/flang/include/flang/Optimizer/Dialect/FIRAttr.td
index 7bd96ac3ea631..8a8c60ff0722b 100644
--- a/flang/include/flang/Optimizer/Dialect/FIRAttr.td
+++ b/flang/include/flang/Optimizer/Dialect/FIRAttr.td
@@ -219,4 +219,24 @@ def LocalitySpecifierTypeAttr : EnumAttr<FIROpsDialect, LocalitySpecifierType,
let assemblyFormat = "`{` `type` `=` $value `}`";
}
+/// Fortran inline attribute
+def FIRinlineNone : I32BitEnumAttrCaseNone<"none">;
+def FIRinlineNo : I32BitEnumAttrCaseBit<"no_inline", 0>;
+def FIRinlineAlways : I32BitEnumAttrCaseBit<"always_inline", 1>;
+def FIRinlineHint : I32BitEnumAttrCaseBit<"inline_hint", 2>;
+
+def fir_FortranInlineEnum
+ : I32BitEnumAttr<"FortranInlineEnum", "Fortran inline attributes",
+ [FIRinlineNone, FIRinlineNo, FIRinlineAlways,
+ FIRinlineHint]> {
+ let separator = ", ";
+ let cppNamespace = "::fir";
+ let genSpecializedAttr = 0;
+ let printBitEnumPrimaryGroups = 1;
+}
+
+def fir_FortranInlineAttr
+ : EnumAttr<FIROpsDialect, fir_FortranInlineEnum, "inline_attrs"> {
+ let assemblyFormat = "`<` $value `>`";
+}
#endif // FIR_DIALECT_FIR_ATTRS
diff --git a/flang/include/flang/Optimizer/Dialect/FIROps.td b/flang/include/flang/Optimizer/Dialect/FIROps.td
index 86502c6271d23..58a317cf5d691 100644
--- a/flang/include/flang/Optimizer/Dialect/FIROps.td
+++ b/flang/include/flang/Optimizer/Dialect/FIROps.td
@@ -2549,6 +2549,7 @@ def fir_CallOp : fir_Op<"call",
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs,
OptionalAttr<fir_FortranProcedureFlagsAttr>:$procedure_attrs,
+ OptionalAttr<fir_FortranInlineAttr>:$inline_attr,
DefaultValuedAttr<Arith_FastMathAttr,
"::mlir::arith::FastMathFlags::none">:$fastmath
);
diff --git a/flang/include/flang/Parser/dump-parse-tree.h b/flang/include/flang/Parser/dump-parse-tree.h
index 5677277a9b381..046cb9b11714a 100644
--- a/flang/include/flang/Parser/dump-parse-tree.h
+++ b/flang/include/flang/Parser/dump-parse-tree.h
@@ -206,8 +206,11 @@ class ParseTreeDumper {
NODE(parser, CompilerDirective)
NODE(CompilerDirective, AssumeAligned)
NODE(CompilerDirective, IgnoreTKR)
+ NODE(CompilerDirective, Inline)
+ NODE(CompilerDirective, ForceInline)
NODE(CompilerDirective, LoopCount)
NODE(CompilerDirective, NameValue)
+ NODE(CompilerDirective, NoInline)
NODE(CompilerDirective, Unrecognized)
NODE(CompilerDirective, VectorAlways)
NODE(CompilerDirective, Unroll)
diff --git a/flang/include/flang/Parser/parse-tree.h b/flang/include/flang/Parser/parse-tree.h
index 6dd4f2492cf22..dcd1a3d542e7f 100644
--- a/flang/include/flang/Parser/parse-tree.h
+++ b/flang/include/flang/Parser/parse-tree.h
@@ -3356,6 +3356,9 @@ struct StmtFunctionStmt {
// !DIR$ NOVECTOR
// !DIR$ NOUNROLL
// !DIR$ NOUNROLL_AND_JAM
+// !DIR$ FORCEINLINE
+// !DIR$ INLINE
+// !DIR$ NOINLINE
// !DIR$ <anything else>
struct CompilerDirective {
UNION_CLASS_BOILERPLATE(CompilerDirective);
@@ -3384,11 +3387,14 @@ struct CompilerDirective {
EMPTY_CLASS(NoVector);
EMPTY_CLASS(NoUnroll);
EMPTY_CLASS(NoUnrollAndJam);
+ EMPTY_CLASS(ForceInline);
+ EMPTY_CLASS(Inline);
+ EMPTY_CLASS(NoInline);
EMPTY_CLASS(Unrecognized);
CharBlock source;
std::variant<std::list<IgnoreTKR>, LoopCount, std::list<AssumeAligned>,
VectorAlways, std::list<NameValue>, Unroll, UnrollAndJam, Unrecognized,
- NoVector, NoUnroll, NoUnrollAndJam>
+ NoVector, NoUnroll, NoUnrollAndJam, ForceInline, Inline, NoInline>
u;
};
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index a516a44204cac..2680fa50a7261 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -1884,6 +1884,26 @@ class FirConverter : public Fortran::lower::AbstractConverter {
setCurrentPosition(stmt.source);
assert(stmt.typedCall && "Call was not analyzed");
mlir::Value res{};
+
+ // Set 'no_inline', 'inline_hint' or 'always_inline' to true on the
+ // ProcedureRef. The NoInline and AlwaysInline attribute will be set in
+ // genProcedureRef later.
+ for (const auto *dir : eval.dirs) {
+ Fortran::common::visit(
+ Fortran::common::visitors{
+ [&](const Fortran::parser::CompilerDirective::ForceInline &) {
+ stmt.typedCall->set_alwaysInline(true);
+ },
+ [&](const Fortran::parser::CompilerDirective::Inline &) {
+ stmt.typedCall->set_inlineHint(true);
+ },
+ [&](const Fortran::parser::CompilerDirective::NoInline &) {
+ stmt.typedCall->set_noInline(true);
+ },
+ [&](const auto &) {}},
+ dir->u);
+ }
+
if (lowerToHighLevelFIR()) {
std::optional<mlir::Type> resultType;
if (stmt.typedCall->hasAlternateReturns())
@@ -2200,6 +2220,50 @@ class FirConverter : public Fortran::lower::AbstractConverter {
// so no clean-up needs to be generated for these entities.
}
+ void attachInlineAttributes(
+ mlir::Operation &op,
+ const llvm::ArrayRef<const Fortran::parser::CompilerDirective *> &dirs) {
+ if (dirs.empty())
+ return;
+
+ for (mlir::Value operand : op.getOperands()) {
+ if (operand.getDefiningOp())
+ attachInlineAttributes(*operand.getDefiningOp(), dirs);
+ }
+
+ if (fir::CallOp callOp = mlir::dyn_cast<fir::CallOp>(op)) {
+ for (const auto *dir : dirs) {
+ Fortran::common::visit(
+ Fortran::common::visitors{
+ [&](const Fortran::parser::CompilerDirective::NoInline &) {
+ callOp.setInlineAttr(fir::FortranInlineEnum::no_inline);
+ },
+ [&](const Fortran::parser::CompilerDirective::Inline &) {
+ callOp.setInlineAttr(fir::FortranInlineEnum::inline_hint);
+ },
+ [&](const Fortran::parser::CompilerDirective::ForceInline &) {
+ callOp.setInlineAttr(fir::FortranInlineEnum::always_inline);
+ },
+ [&](const auto &) {}},
+ dir->u);
+ }
+ }
+ }
+
+ void attachAttributesToDoLoopOperations(
+ fir::DoLoopOp &doLoop,
+ llvm::SmallVectorImpl<const Fortran::parser::CompilerDirective *> &dirs) {
+ if (!doLoop.getOperation() || dirs.empty())
+ return;
+
+ for (mlir::Block &block : doLoop.getRegion()) {
+ for (mlir::Operation &op : block.getOperations()) {
+ if (!dirs.empty())
+ attachInlineAttributes(op, dirs);
+ }
+ }
+ }
+
/// Generate FIR for a DO construct. There are six variants:
/// - unstructured infinite and while loops
/// - structured and unstructured increment loops
@@ -2351,6 +2415,11 @@ class FirConverter : public Fortran::lower::AbstractConverter {
if (!incrementLoopNestInfo.empty() &&
incrementLoopNestInfo.back().isConcurrent)
localSymbols.popScope();
+
+ // Add attribute(s) on operations in fir::DoLoopOp if necessary
+ for (IncrementLoopInfo &info : incrementLoopNestInfo)
+ if(auto loopOp = mlir::dyn_cast_if_present<fir::DoLoopOp>(info.loopOp))
+ attachAttributesToDoLoopOperations(loopOp, doStmtEval.dirs);
}
/// Generate FIR to evaluate loop control values (lower, upper and step).
@@ -3154,6 +3223,26 @@ class FirConverter : public Fortran::lower::AbstractConverter {
e->dirs.push_back(&dir);
}
+ void
+ attachInliningDirectiveToStmt(const Fortran::parser::CompilerDirective &dir,
+ Fortran::lower::pft::Evaluation *e) {
+ while (e->isDirective())
+ e = e->lexicalSuccessor;
+
+ // If the successor is a statement or a do loop, the compiler
+ // will perform inlining.
+ if (e->isA<Fortran::parser::CallStmt>() ||
+ e->isA<Fortran::parser::NonLabelDoStmt>() ||
+ e->isA<Fortran::parser::AssignmentStmt>()) {
+ e->dirs.push_back(&dir);
+ } else {
+ mlir::Location loc = toLocation();
+ mlir::emitWarning(loc,
+ "Inlining directive not in front of loops, function"
+ "call or assignment.\n");
+ }
+ }
+
void genFIR(const Fortran::parser::CompilerDirective &dir) {
Fortran::lower::pft::Evaluation &eval = getEval();
@@ -3177,6 +3266,15 @@ class FirConverter : public Fortran::lower::AbstractConverter {
[&](const Fortran::parser::CompilerDirective::NoUnrollAndJam &) {
attachDirectiveToLoop(dir, &eval);
},
+ [&](const Fortran::parser::CompilerDirective::ForceInline &) {
+ attachInliningDirectiveToStmt(dir, &eval);
+ },
+ [&](const Fortran::parser::CompilerDirective::Inline &) {
+ attachInliningDirectiveToStmt(dir, &eval);
+ },
+ [&](const Fortran::parser::CompilerDirective::NoInline &) {
+ attachInliningDirectiveToStmt(dir, &eval);
+ },
[&](const auto &) {}},
dir.u);
}
@@ -5086,7 +5184,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
void genDataAssignment(
const Fortran::evaluate::Assignment &assign,
- const Fortran::evaluate::ProcedureRef *userDefinedAssignment) {
+ const Fortran::evaluate::ProcedureRef *userDefinedAssignment,
+ const llvm::ArrayRef<const Fortran::parser::CompilerDirective *> &dirs =
+ {}) {
mlir::Location loc = getCurrentLocation();
fir::FirOpBuilder &builder = getFirOpBuilder();
@@ -5166,10 +5266,20 @@ class FirConverter : public Fortran::lower::AbstractConverter {
genCUDADataTransfer(builder, loc, assign, lhs, rhs,
isWholeAllocatableAssignment,
keepLhsLengthInAllocatableAssignment);
- else
+ else {
+ // If RHS or LHS have a CallOp in their expression, this operation will
+ // have the 'no_inline' or 'always_inline' attribute if there is a
+ // directive just before the assignement.
+ if (!dirs.empty()) {
+ if (rhs.getDefiningOp())
+ attachInlineAttributes(*rhs.getDefiningOp(), dirs);
+ if (lhs.getDefiningOp())
+ attachInlineAttributes(*lhs.getDefiningOp(), dirs);
+ }
hlfir::AssignOp::create(builder, loc, rhs, lhs,
isWholeAllocatableAssignment,
keepLhsLengthInAllocatableAssignment);
+ }
if (hasCUDAImplicitTransfer && !isInDeviceContext) {
localSymbols.popScope();
for (mlir::Value temp : implicitTemps)
@@ -5237,16 +5347,21 @@ class FirConverter : public Fortran::lower::AbstractConverter {
}
/// Shared for both assignments and pointer assignments.
- void genAssignment(const Fortran::evaluate::Assignment &assign) {
+ void
+ genAssignment(const Fortran::evaluate::Assignment &assign,
+ const llvm::ArrayRef<const Fortran::parser::CompilerDirective *>
+ &dirs = {}) {
mlir::Location loc = toLocation();
if (lowerToHighLevelFIR()) {
Fortran::common::visit(
Fortran::common::visitors{
[&](const Fortran::evaluate::Assignment::Intrinsic &) {
- genDataAssignment(assign, /*userDefinedAssignment=*/nullptr);
+ genDataAssignment(assign, /*userDefinedAssignment=*/nullptr,
+ dirs);
},
[&](const Fortran::evaluate::ProcedureRef &procRef) {
- genDataAssignment(assign, /*userDefinedAssignment=*/&procRef);
+ genDataAssignment(assign, /*userDefinedAssignment=*/&procRef,
+ dirs);
},
[&](const Fortran::evaluate::Assignment::BoundsSpec &lbExprs) {
if (isInsideHlfirForallOrWhere())
@@ -5651,7 +5766,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
}
void genFIR(const Fortran::parser::AssignmentStmt &stmt) {
- genAssignment(*stmt.typedAssignment->v);
+ Fortran::lower::pft::Evaluation &eval = getEval();
+ genAssignment(*stmt.typedAssignment->v, eval.dirs);
}
void genFIR(const Fortran::parser::SyncAllStmt &stmt) {
diff --git a/flang/lib/Lower/ConvertCall.cpp b/flang/lib/Lower/ConvertCall.cpp
index fb72040f9ea14..9bf994e70cf5d 100644
--- a/flang/lib/Lower/ConvertCall.cpp
+++ b/flang/lib/Lower/ConvertCall.cpp
@@ -700,9 +700,20 @@ Fortran::lower::genCallOpAndResult(
callResult = dispatch.getResult(0);
} else {
// Standard procedure call with fir.call.
+ fir::FortranInlineEnumAttr inlineAttr;
+
+ if (caller.getCallDescription().hasNoInline())
+ inlineAttr = fir::FortranInlineEnumAttr::get(
+ builder.getContext(), fir::FortranInlineEnum::no_inline);
+ else if (caller.getCallDescription().hasInlineHint())
+ inlineAttr = fir::FortranInlineEnumAttr::get(
+ builder.getContext(), fir::FortranInlineEnum::inline_hint);
+ else if (caller.getCallDescription().hasAlwaysInline())
+ inlineAttr = fir::FortranInlineEnumAttr::get(
+ builder.getContext(), fir::FortranInlineEnum::always_inline);
auto call = fir::CallOp::create(
builder, loc, funcType.getResults(), funcSymbolAttr, operands,
- /*arg_attrs=*/nullptr, /*res_attrs=*/nullptr, procAttrs);
+ /*arg_attrs=*/nullptr, /*res_attrs=*/nullptr, procAttrs, inlineAttr);
callNumResults = call.getNumResults();
if (callNumResults != 0)
diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index 478ab151b96d0..ca4aefb653d2a 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -680,6 +680,18 @@ struct CallOpConversion : public fir::FIROpConversion<fir::CallOp> {
if (mlir::ArrayAttr resAttrs = call.getResAttrsAttr())
llvmCall.setResAttrsAttr(resAttrs);
+ if (auto inlineAttr = call.getInlineAttrAttr()) {
+ llvmCall->removeAttr("inline_attr");
+ if (inlineAttr.getValue() == fir::FortranInlineEnum::no_inline) {
+ llvmCall.setNoInlineAttr(rewriter.getUnitAttr());
+ } else if (inlineAttr.getValue() == fir::FortranInlineEnum::inline_hint) {
+ llvmCall.setInlineHintAttr(rewriter.getUnitAttr());
+ } else if (inlineAttr.getValue() ==
+ fir::FortranInlineEnum::always_inline) {
+ llvmCall.setAlwaysInlineAttr(rewriter.getUnitAttr());
+ }
+ }
+
if (memAttr)
llvmCall.setMemoryEffectsAttr(
mlir::cast<mlir::LLVM::MemoryEffectsAttr>(memAttr));
diff --git a/flang/lib/Optimizer/Transforms/PolymorphicOpConversion.cpp b/flang/lib/Optimizer/Transforms/PolymorphicOpConversion.cpp
index 25a8f7af5b0a2..8c0acc517c18f 100644
--- a/flang/lib/Optimizer/Transforms/PolymorphicOpConversion.cpp
+++ b/flang/lib/Optimizer/Transforms/PolymorphicOpConversion.cpp
@@ -246,7 +246,8 @@ struct DispatchOpConv : public OpConversionPattern<fir::DispatchOp> {
args.append(dispatch.getArgs().begin(), dispatch.getArgs().end());
rewriter.replaceOpWithNewOp<fir::CallOp>(
dispatch, resTypes, nullptr, args, dispatch.getArgAttrsAttr(),
- dispatch.getResAttrsAttr(), dispatch.getProcedureAttrsAttr());
+ dispatch.getResAttrsAttr(), dispatch.getProcedureAttrsAttr(),
+ /*inline_attr*/ fir::FortranInlineEnumAttr{});
return mlir::success();
}
diff --git a/flang/lib/Parser/Fortran-parsers.cpp b/flang/lib/Parser/Fortran-parsers.cpp
index d33a18fe9572c..59fe7d813d96a 100644
--- a/flang/lib/Parser/Fortran-parsers.cpp
+++ b/flang/lib/Parser/Fortran-parsers.cpp
@@ -1314,6 +1314,11 @@ constexpr auto novector{"NOVECTOR" >> construct<CompilerDirective::NoVector>()};
constexpr auto nounroll{"NOUNROLL" >> construct<CompilerDirective::NoUnroll>()};
constexpr auto nounrollAndJam{
"NOUNROLL_AND_JAM" >> construct<CompilerDirective::NoUnrollAndJam>()};
+constexpr auto forceinlineDir{
+ "FORCEINLINE" >> construct<CompilerDirective::ForceInline>()};
+constexpr auto noinlineDir{
+ "NOINLINE" >> construct<CompilerDirective::NoInline>()};
+constexpr auto inlineDir{"INLINE" >> construct<CompilerDirective::Inline>()};
TYPE_PARSER(beginDirective >> "DIR$ "_tok >>
sourced((construct<CompilerDirective>(ignore_tkr) ||
construct<CompilerDirective>(loopCount) ||
@@ -1324,6 +1329,9 @@ TYPE_PARSER(beginDirective >> "DIR$ "_tok >>
construct<CompilerDirective>(novector) ||
construct<CompilerDirective>(nounrollAndJam) ||
construct<CompilerDirective>(nounroll) ||
+ construct<CompilerDirective>(noinlineDir) ||
+ construct<CompilerDirective>(forceinlineDir) ||
+ construct<CompilerDirective>(inlineDir) ||
construct<CompilerDirective>(
many(construct<CompilerDirective::NameValue>(
name, maybe(("="_tok || ":"_tok) >> digitString64))))) /
diff --git a/flang/lib/Parser/unparse.cpp b/flang/lib/Parser/unparse.cpp
index b172e429c84e8..2821fc78e3713 100644
--- a/flang/lib/Parser/unparse.cpp
+++ b/flang/lib/Parser/unparse.cpp
@@ -1867,6 +1867,13 @@ class UnparseVisitor {
[&](const CompilerDirective::NoUnrollAndJam &) {
Word("!DIR$ NOUNROLL_AND_JAM");
},
+ [&](const CompilerDirective::ForceInline &) {
+ Word("!DIR$ FORCEINLINE");
+ },
+ [&](const CompilerDirective::Inline &) { Word("!DIR$ INLINE"); },
+ [&](const CompilerDirective::NoInline &) {
+ Word("!DIR$ NOINLINE");
+ },
[&](const CompilerDirective::Unrecognized &) {
Word("!DIR$ ");
Word(x.source.ToString());
diff --git a/flang/lib/Semantics/canonicalize-directives.cpp b/flang/lib/Semantics/canonicalize-directives.cpp
index 104df253ab642..a651a873d3b40 100644
--- a/flang/lib/Semantics/canonicalize-directives.cpp
+++ b/flang/lib/Semantics/canonicalize-directives.cpp
@@ -60,7 +60,11 @@ static bool IsExecutionDirective(const parser::CompilerDirective &dir) {
std::holds_alternative<parser::CompilerDirective::UnrollAndJam>(dir.u) ||
std::holds_alternative<parser::CompilerDirective::NoVector>(dir.u) ||
std::holds_alternative<parser::CompilerDirective::NoUnroll>(dir.u) ||
- std::holds_alternative<parser::CompilerDirective::NoUnrollAndJam>(dir.u);
+ std::holds_alternative<parser::CompilerDirective::NoUnrollAndJam>(
+ dir.u) ||
+ std::holds_alternative<parser::CompilerDirective::ForceInline>(dir.u) ||
+ std::holds_alternative<parser::CompilerDirective::Inline>(dir.u) ||
+ std::holds_alternative<parser::CompilerDirective::NoInline>(dir.u);
}
void CanonicalizationOfDirectives::Post(parser::SpecificationPart &spec) {
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index db75437708a6c..1fce5bee799e7 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -10078,7 +10078,10 @@ void ResolveNamesVisitor::Post(const parser::CompilerDirective &x) {
std::holds_alternative<parser::CompilerDirective::UnrollAndJam>(x.u) ||
std::holds_alternative<parser::CompilerDirective::NoVector>(x.u) ||
std::holds_alternative<parser::CompilerDirective::NoUnroll>(x.u) ||
- std::holds_alternative<parser::CompilerDirective::NoUnrollAndJam>(x.u)) {
+ std::holds_alternative<parser::CompilerDirective::NoUnrollAndJam>(x.u) ||
+ std::holds_alternative<parser::CompilerDirective::ForceInline>(x.u) ||
+ std::holds_alternative<parser::CompilerDirective::Inline>(x.u) ||
+ std::holds_alternative<parser::CompilerDirective::NoInline>(x.u)) {
return;
}
if (const auto *tkr{
diff --git a/flang/test/Integration/inline_directive.f90 b/flang/test/Integration/inline_directive.f90
new file mode 100644
index 0000000000000..1f053846778cd
--- /dev/null
+++ b/flang/test/Integration/inline_directive.f90
@@ -0,0 +1,69 @@
+! This directory can be used to add Integration tests involving multiple stages of the compiler (for eg. from Fortran to LLVM IR).
+! It should not contain executable tests. We should only add tests here sparingly and only if there is no other way to test.
+! RUN: %flang_fc1 -emit-llvm -o - %s | FileCheck %s
+
+! CHECK-LABEL: test_inline
+subroutine test_inline()
+ integer :: x, y
+!CHECK: %[[VAL_1:.*]] = alloca i32, i64 1, align 4
+!CHECK: %[[VAL_2:.*]] = alloca i32, i64 1, align 4
+!CHECK: %[[VAL_3:.*]] = alloca i32, i64 1, align 4
+!CHECK: %[[VAL_4:.*]] = alloca i32, i64 1, align 4
+
+ !dir$ forceinline
+ y = g(x)
+ !dir$ forceinline
+ call f(x, y)
+!CHECK: %[[VAL_5:.*]] = load i32, ptr %[[VAL_3]], align 4
+!CHECK: %[[VAL_6:.*]] = mul i32 %[[VAL_5]], 2
+!CHECK: store i32 %6, ptr %[[VAL_1]], align 4
+!CHECK: %[[VAL_7:.*]] = load i32, ptr %[[VAL_1]], align 4
+!CHECK: store i32 %7, ptr %[[VAL_2]], align 4
+!CHECK: %[[VAL_8:.]] = load i32, ptr %[[VAL_3]], align 4
+!CHECK: %[[VAL_9:.]] = mul i32 %[[VAL_8]], 2
+!CHECK: store i32 %9, ptr %[[VAL_2]], align 4
+
+ !dir$ inline
+ y = g(x)
+ !dir$ inline
+ call f(x, y)
+!CHECK: %[[VAL_10:.*]] = call i32 @_QFtest_inlinePg(ptr %[[VAL_3]]) #[[INLINE:.*]]
+!CHECK: store i32 %[[VAL_10]], ptr %[[VAL_2]], align 4
+!CHECK: call void @_QFtest_inlinePf(ptr %[[VAL_3]], ptr %[[VAL_2]]) #[[INLINE]]
+
+ !dir$ inline
+ do i = 1, 100
+ call f(x, y)
+ !CHECK: br i1 %[[VAL_14:.*]], label %[[VAL_15:.*]], label %[[VAL_19:.*]]
+ !CHECK: call void @_QFtest_inlinePf(ptr %[[VAL_3]], ptr %[[VAL_2]]) #[[INLINE]]
+ enddo
+
+ !dir$ noinline
+ y = g(x)
+ !dir$ noinline
+ call f(x, y)
+!CHECK: %[[VAL_10:.*]] = call i32 @_QFtest_inlinePg(ptr %[[VAL_3]]) #[[NOINLINE:.*]]
+!CHECK: store i32 %[[VAL_10]], ptr %[[VAL_2]], align 4
+!CHECK: call void @_QFtest_inlinePf(ptr %[[VAL_3]], ptr %[[VAL_2]]) #[[NOINLINE]]
+
+ !dir$ noinline
+ do i = 1, 100
+ call f(x, y)
+ !CHECK: br i1 %[[VAL_14:.*]], label %[[VAL_15:.*]], label %[[VAL_19:.*]]
+ !CHECK: call void @_QFtest_inlinePf(ptr %[[VAL_3]], ptr %[[VAL_2]]) #[[NOINLINE]]
+ enddo
+
+ contains
+ subroutine f(x, y)
+ integer, intent(in) :: x
+ integer, intent(out) :: y
+ y = x*2
+ end subroutine f
+ integer function g(x)
+ integer :: x
+ g = x*2
+ end function g
+end subroutine test_inline
+
+!CHECK: attributes #[[INLINE]] = { inlinehint }
+!CHECK: attributes #[[NOINLINE]] = { noinline }
diff --git a/flang/test/Lower/inline_directive.f90 b/flang/test/Lower/inline_directive.f90
new file mode 100644
index 0000000000000..347df85f05dda
--- /dev/null
+++ b/flang/test/Lower/inline_directive.f90
@@ -0,0 +1,61 @@
+! RUN: %flang_fc1 -emit-fir -o - %s | FileCheck %s
+
+subroutine test_inline()
+ integer :: x, y
+!CHECK: %[[VAL_0:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFtest_inlineEx"}
+!CHECK: %[[VAL_1:.*]] = fir.declare %[[VAL_0]] {uniq_name = "_QFtest_inlineEx"} : (!fir.ref<i32>) -> !fir.ref<i32>
+!CHECK: %[[VAL_2:.*]] = fir.alloca i32 {bindc_name = "y", uniq_name = "_QFtest_inlineEy"}
+!CHECK: %[[VAL_3:.*]] = fir.declare %[[VAL_2]] {uniq_name = "_QFtest_inlineEy"} : (!fir.ref<i32>) -> !fir.ref<i32>
+
+ !dir$ forceinline
+ y = g(x)
+ !CHECK: %[[VAL_4:.*]] = fir.call @_QFtest_inlinePg(%[[VAL_1]]) fastmath<contract> {inline_attr = #fir.inline_attrs<always_inline>} : (!fir.ref<i32>) -> i32
+ !CHECK: fir.store %[[VAL_4]] to %[[VAL_3]] : !fir.ref<i32>
+
+ !dir$ forceinline
+ call f(x, y)
+ !CHECK: fir.call @_QFtest_inlinePf(%[[VAL_1]], %[[VAL_3]]) fastmath<contract> {inline_attr = #fir.inline_attrs<always_inline>} : (!fir.ref<i32>, !fir.ref<i32>) -> ()
+
+ !dir$ noinline
+ y = g(x) + 7 * (8 + g(y))
+ !CHECK: %[[VAL_8:.*]] = fir.call @_QFtest_inlinePg(%[[VAL_1]]) fastmath<contract> {inline_attr = #fir.inline_attrs<no_inline>} : (!fir.ref<i32>) -> i32
+ !CHECK: %[[VAL_9:.*]] = fir.call @_QFtest_inlinePg(%[[VAL_3]]) fastmath<contract> {inline_attr = #fir.inline_attrs<no_inline>} : (!fir.ref<i32>) -> i32
+ !CHECK: %[[VAL_10:.*]] = arith.addi %[[VAL_9]], %[[C8:.*]] : i32
+ !CHECK: %[[VAL_11:.*]] = fir.no_reassoc %[[VAL_10]] : i32
+ !CHECK: %[[VAL_12:.*]] = arith.muli %[[VAL_11]], %[[C7:.*]] : i32
+ !CHECK: %[[VAL_13:.*]] = arith.addi %[[VAL_8]], %[[VAL_12]] : i32
+ !CHECK: fir.store %[[VAL_13]] to %[[VAL_3]] : !fir.ref<i32>
+
+ !dir$ noinline
+ call f(x, y)
+ !CHECK: fir.call @_QFtest_inlinePf(%[[VAL_1]], %[[VAL_3]]) fastmath<contract> {inline_attr = #fir.inline_attrs<no_inline>} : (!fir.ref<i32>, !fir.ref<i32>) -> ()
+
+ !dir$ inline
+ call f(x, y)
+ !CHECK: fir.call @_QFtest_inlinePf(%[[VAL_1]], %[[VAL_3]]) fastmath<contract> {inline_attr = #fir.inline_attrs<inline_hint>} : (!fir.ref<i32>, !fir.ref<i32>) -> ()
+
+ !dir$ forceinline
+ do i = 1, 100
+ !CHECK: fir.do_loop %[[ARG_0:.*]] = %[[FROM:.*]] to %[[TO:.*]] step %[[C1:.*]] iter_args(%[[ARG_1:.*]] = {{.*}}) -> (i32) {
+ !CHECK: fir.call @_QFtest_inlinePf(%[[VAL_1]], %[[VAL_3]]) fastmath<contract> {inline_attr = #fir.inline_attrs<always_inline>} : (!fir.ref<i32>, !fir.ref<i32>) -> ()
+ call f(x, y)
+ enddo
+
+ !dir$ inline
+ do i = 1, 100
+ !CHECK: fir.do_loop %[[ARG_0:.*]] = %[[FROM:.*]] to %[[TO:.*]] step %[[C1:.*]] iter_args(%[[ARG_1:.*]] = {{.*}}) -> (i32) {
+ !CHECK: fir.call @_QFtest_inlinePf(%[[VAL_1]], %[[VAL_3]]) fastmath<contract> {inline_attr = #fir.inline_attrs<inline_hint>} : (!fir.ref<i32>, !fir.ref<i32>) -> ()
+ call f(x, y)
+ enddo
+!CHECK: return
+ contains
+ subroutine f(x, y)
+ integer, intent(in) :: x
+ integer, intent(out) :: y
+ y = x*2
+ end subroutine f
+ integer function g(x)
+ integer :: x
+ g = x*2
+ end function g
+end subroutine test_inline
diff --git a/flang/test/Parser/compiler-directives.f90 b/flang/test/Parser/compiler-directives.f90
index 04d22ff0fd8ee..b2fe466377f70 100644
--- a/flang/test/Parser/compiler-directives.f90
+++ b/flang/test/Parser/compiler-directives.f90
@@ -72,3 +72,27 @@ subroutine no_vector
do i=1,10
enddo
end subroutine
+
+subroutine inline
+ integer :: a
+ !dir$ forceinline
+ ! CHECK: !DIR$ FORCEINLINE
+ a = f(2)
+
+ !dir$ inline
+ ! CHECK: !DIR$ INLINE
+ call g()
+
+ !dir$ noinline
+ ! CHECK: !DIR$ NOINLINE
+ call g()
+
+ contains
+ function f(x)
+ integer :: x
+ f = x**2
+ end function
+
+ subroutine g()
+ end subroutine
+end subroutine
>From fe5b43c07a8507d183d5ce2e8ee04ef799007bc0 Mon Sep 17 00:00:00 2001
From: Jean-Didier Pailleux <jean-didier.pailleux at sipearl.com>
Date: Mon, 27 Oct 2025 09:56:23 +0100
Subject: [PATCH 2/3] Update Directives.md with comments from Kelvin Li
---
flang/docs/Directives.md | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/flang/docs/Directives.md b/flang/docs/Directives.md
index 91c27cb510ea0..3ebb08c486228 100644
--- a/flang/docs/Directives.md
+++ b/flang/docs/Directives.md
@@ -53,6 +53,14 @@ A list of non-standard directives supported by Flang
* `!dir$ novector` disabling vectorization on the following loop.
* `!dir$ nounroll` disabling unrolling on the following loop.
* `!dir$ nounroll_and_jam` disabling unrolling and jamming on the following loop.
+* `!dir$ inline` instructs the compiler to attempt to inline the called routines if the
+ directive is specified before a call statement or all call statements within the loop
+ body if specified before a DO LOOP or all function references if specified before an
+ assignment statement.
+* `!dir$ forceinline` works in the same way as the `inline` directive, but it forces
+ inlining by the compiler on a function call statement.
+* `!dir$ noinline` works in the same way as the `inline` directive, but prevents
+ any attempt of inlining by the compiler on a function call statement.
# Directive Details
>From c448c969215764d5ccc103b45f1a682a75517bd7 Mon Sep 17 00:00:00 2001
From: Jean-Didier Pailleux <jean-didier.pailleux at sipearl.com>
Date: Mon, 27 Oct 2025 10:11:17 +0100
Subject: [PATCH 3/3] Fix formatting + Matching style in naming
---
flang/include/flang/Evaluate/call.h | 6 +++---
flang/lib/Lower/Bridge.cpp | 8 ++++----
2 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/flang/include/flang/Evaluate/call.h b/flang/include/flang/Evaluate/call.h
index b09f203c0de37..a7a66c6769aa0 100644
--- a/flang/include/flang/Evaluate/call.h
+++ b/flang/include/flang/Evaluate/call.h
@@ -255,11 +255,11 @@ class ProcedureRef {
bool hasAlternateReturns() const { return hasAlternateReturns_; }
bool hasNoInline() const { return noInline_; }
- void set_noInline(bool ni) { noInline_ = ni; }
+ void setNoInline(bool ni) { noInline_ = ni; }
bool hasAlwaysInline() const { return alwaysInline_; }
- void set_alwaysInline(bool ai) { alwaysInline_ = ai; }
+ void setAlwaysInline(bool ai) { alwaysInline_ = ai; }
bool hasInlineHint() const { return inlineHint_; }
- void set_inlineHint(bool ih) { inlineHint_ = ih; }
+ void setInlineHint(bool ih) { inlineHint_ = ih; }
Expr<SomeType> *UnwrapArgExpr(int n) {
if (static_cast<std::size_t>(n) < arguments_.size() && arguments_[n]) {
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 2680fa50a7261..6e729874eb5e6 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -1892,13 +1892,13 @@ class FirConverter : public Fortran::lower::AbstractConverter {
Fortran::common::visit(
Fortran::common::visitors{
[&](const Fortran::parser::CompilerDirective::ForceInline &) {
- stmt.typedCall->set_alwaysInline(true);
+ stmt.typedCall->setAlwaysInline(true);
},
[&](const Fortran::parser::CompilerDirective::Inline &) {
- stmt.typedCall->set_inlineHint(true);
+ stmt.typedCall->setInlineHint(true);
},
[&](const Fortran::parser::CompilerDirective::NoInline &) {
- stmt.typedCall->set_noInline(true);
+ stmt.typedCall->setNoInline(true);
},
[&](const auto &) {}},
dir->u);
@@ -2418,7 +2418,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
// Add attribute(s) on operations in fir::DoLoopOp if necessary
for (IncrementLoopInfo &info : incrementLoopNestInfo)
- if(auto loopOp = mlir::dyn_cast_if_present<fir::DoLoopOp>(info.loopOp))
+ if (auto loopOp = mlir::dyn_cast_if_present<fir::DoLoopOp>(info.loopOp))
attachAttributesToDoLoopOperations(loopOp, doStmtEval.dirs);
}
More information about the flang-commits
mailing list