Skip to content

Commit a1701f9

Browse files
committed
[Flang][Lower] Attach target_cpu and target_features attributes to MLIR functions
This patch forwards the target CPU and features information from the Flang frontend to MLIR func.func operation attributes, which are later used to populate the target_cpu and target_features llvm.func attributes. This completes a full flow by which target CPU and features make it all the way from compiler options to LLVM IR function attributes.
1 parent ae33bcc commit a1701f9

File tree

7 files changed

+73
-19
lines changed

7 files changed

+73
-19
lines changed

flang/include/flang/Lower/Bridge.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
#include "flang/Optimizer/Builder/FIRBuilder.h"
2222
#include "flang/Optimizer/Dialect/Support/KindMapping.h"
2323
#include "mlir/IR/BuiltinOps.h"
24-
25-
namespace llvm {
26-
class DataLayout;
27-
} // namespace llvm
24+
#include "llvm/Target/TargetMachine.h"
2825

2926
namespace Fortran {
3027
namespace common {
@@ -64,11 +61,11 @@ class LoweringBridge {
6461
const Fortran::lower::LoweringOptions &loweringOptions,
6562
const std::vector<Fortran::lower::EnvironmentDefault> &envDefaults,
6663
const Fortran::common::LanguageFeatureControl &languageFeatures,
67-
const llvm::DataLayout *dataLayout = nullptr) {
64+
const llvm::TargetMachine &targetMachine) {
6865
return LoweringBridge(ctx, semanticsContext, defaultKinds, intrinsics,
6966
targetCharacteristics, allCooked, triple, kindMap,
7067
loweringOptions, envDefaults, languageFeatures,
71-
dataLayout);
68+
targetMachine);
7269
}
7370

7471
//===--------------------------------------------------------------------===//
@@ -110,6 +107,8 @@ class LoweringBridge {
110107
return languageFeatures;
111108
}
112109

110+
const llvm::TargetMachine &getTargetMachine() const { return targetMachine; }
111+
113112
/// Create a folding context. Careful: this is very expensive.
114113
Fortran::evaluate::FoldingContext createFoldingContext() const;
115114

@@ -147,7 +146,7 @@ class LoweringBridge {
147146
const Fortran::lower::LoweringOptions &loweringOptions,
148147
const std::vector<Fortran::lower::EnvironmentDefault> &envDefaults,
149148
const Fortran::common::LanguageFeatureControl &languageFeatures,
150-
const llvm::DataLayout *dataLayout);
149+
const llvm::TargetMachine &targetMachine);
151150
LoweringBridge() = delete;
152151
LoweringBridge(const LoweringBridge &) = delete;
153152

@@ -164,6 +163,7 @@ class LoweringBridge {
164163
const Fortran::lower::LoweringOptions &loweringOptions;
165164
const std::vector<Fortran::lower::EnvironmentDefault> &envDefaults;
166165
const Fortran::common::LanguageFeatureControl &languageFeatures;
166+
const llvm::TargetMachine &targetMachine;
167167
};
168168

169169
} // namespace lower

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,14 @@ bool CodeGenAction::beginSourceFileAction() {
285285
ci.getSemanticsContext().defaultKinds();
286286
fir::KindMapping kindMap(mlirCtx.get(), llvm::ArrayRef<fir::KindTy>{
287287
fir::fromDefaultKinds(defKinds)});
288-
const llvm::DataLayout &dl = targetMachine.createDataLayout();
289-
290288
lower::LoweringBridge lb = Fortran::lower::LoweringBridge::create(
291289
*mlirCtx, ci.getSemanticsContext(), defKinds,
292290
ci.getSemanticsContext().intrinsics(),
293291
ci.getSemanticsContext().targetCharacteristics(),
294292
ci.getParsing().allCooked(), ci.getInvocation().getTargetOpts().triple,
295293
kindMap, ci.getInvocation().getLoweringOpts(),
296294
ci.getInvocation().getFrontendOpts().envDefaults,
297-
ci.getInvocation().getFrontendOpts().features, &dl);
295+
ci.getInvocation().getFrontendOpts().features, targetMachine);
298296

299297
// Fetch module from lb, so we can set
300298
mlirModule = std::make_unique<mlir::ModuleOp>(lb.getModule());

flang/lib/Lower/Bridge.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "flang/Semantics/symbol.h"
5555
#include "flang/Semantics/tools.h"
5656
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
57+
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
5758
#include "mlir/IR/PatternMatch.h"
5859
#include "mlir/Parser/Parser.h"
5960
#include "mlir/Transforms/RegionUtils.h"
@@ -4290,6 +4291,18 @@ class FirConverter : public Fortran::lower::AbstractConverter {
42904291
assert(blockId == 0 && "invalid blockId");
42914292
assert(activeConstructStack.empty() && "invalid construct stack state");
42924293

4294+
// Set target_cpu and target_features attributes to be passed through to the
4295+
// llvm.func operation during lowering.
4296+
const llvm::TargetMachine &targetMachine = bridge.getTargetMachine();
4297+
if (auto targetCPU = targetMachine.getTargetCPU(); !targetCPU.empty())
4298+
func->setAttr("target_cpu",
4299+
mlir::StringAttr::get(func.getContext(), targetCPU));
4300+
4301+
if (auto targetFeatures = targetMachine.getTargetFeatureString();
4302+
!targetFeatures.empty())
4303+
func->setAttr("target_features", mlir::LLVM::TargetFeaturesAttr::get(
4304+
func.getContext(), targetFeatures));
4305+
42934306
// Manage floating point exception, halting mode, and rounding mode
42944307
// settings at function entry and exit.
42954308
if (!funit.isMainProgram())
@@ -5062,12 +5075,12 @@ Fortran::lower::LoweringBridge::LoweringBridge(
50625075
const Fortran::lower::LoweringOptions &loweringOptions,
50635076
const std::vector<Fortran::lower::EnvironmentDefault> &envDefaults,
50645077
const Fortran::common::LanguageFeatureControl &languageFeatures,
5065-
const llvm::DataLayout *dataLayout)
5078+
const llvm::TargetMachine &targetMachine)
50665079
: semanticsContext{semanticsContext}, defaultKinds{defaultKinds},
50675080
intrinsics{intrinsics}, targetCharacteristics{targetCharacteristics},
50685081
cooked{&cooked}, context{context}, kindMap{kindMap},
50695082
loweringOptions{loweringOptions}, envDefaults{envDefaults},
5070-
languageFeatures{languageFeatures} {
5083+
languageFeatures{languageFeatures}, targetMachine{targetMachine} {
50715084
// Register the diagnostic handler.
50725085
context.getDiagEngine().registerHandler([](mlir::Diagnostic &diag) {
50735086
llvm::raw_ostream &os = llvm::errs();
@@ -5118,6 +5131,6 @@ Fortran::lower::LoweringBridge::LoweringBridge(
51185131
assert(module.get() && "module was not created");
51195132
fir::setTargetTriple(*module.get(), triple);
51205133
fir::setKindMapping(*module.get(), kindMap);
5121-
if (dataLayout)
5122-
fir::support::setMLIRDataLayout(*module.get(), *dataLayout);
5134+
fir::support::setMLIRDataLayout(*module.get(),
5135+
targetMachine.createDataLayout());
51235136
}

flang/test/Driver/save-mlir-temps.f90

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151
! Content to check from the MLIR outputs
5252
!--------------------------
5353
! MLIR-FIR-NOT: llvm.func
54-
! MLIR-FIR: func.func @{{.*}}main(){{.*}}{
54+
! MLIR-FIR: func.func @{{.*}}main(){{.*}}
5555

56-
! MLIR-FIR-NOT: func.func
57-
! MLIR-LLVMIR: llvm.func @{{.*}}main(){{.*}}{
56+
! MLIR-LLVMIR-NOT: func.func
57+
! MLIR-LLVMIR: llvm.func @{{.*}}main(){{.*}}
5858

5959
end program
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
! REQUIRES: amdgpu-registered-target
2+
! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes=ALL,NONE
3+
! RUN: %flang_fc1 -emit-fir -triple amdgcn-amd-amdhsa %s -o - | FileCheck %s --check-prefixes=ALL,TRIPLE
4+
! RUN: %flang_fc1 -emit-fir -target-cpu gfx90a %s -o - | FileCheck %s --check-prefixes=ALL,CPU
5+
! RUN: %flang_fc1 -emit-fir -triple amdgcn-amd-amdhsa -target-cpu gfx90a %s -o - | FileCheck %s --check-prefixes=ALL,BOTH
6+
7+
! ALL-LABEL: func.func @_QPfoo()
8+
9+
! NONE-NOT: target_cpu
10+
! NONE-NOT: target_features
11+
12+
! TRIPLE-SAME: target_cpu = "generic-hsa"
13+
! TRIPLE-NOT: target_features
14+
15+
! CPU-SAME: target_cpu = "gfx90a"
16+
! CPU-NOT: target_features
17+
18+
! BOTH-SAME: target_cpu = "gfx90a"
19+
! BOTH-SAME: target_features = #llvm.target_features<[
20+
! BOTH-SAME: "+gfx90a-insts"
21+
! BOTH-SAME: ]>
22+
subroutine foo
23+
end subroutine
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
! REQUIRES: x86-registered-target
2+
! RUN: %flang_fc1 -emit-fir -triple x86_64-unknown-linux-gnu %s -o - | FileCheck %s --check-prefixes=ALL,NONE
3+
! RUN: %flang_fc1 -emit-fir -triple x86_64-unknown-linux-gnu -target-cpu x86-64 %s -o - | FileCheck %s --check-prefixes=ALL,CPU
4+
! RUN: %flang_fc1 -emit-fir -triple x86_64-unknown-linux-gnu -target-feature +sse %s -o - | FileCheck %s --check-prefixes=ALL,FEATURE
5+
! RUN: %flang_fc1 -emit-fir -triple x86_64-unknown-linux-gnu -target-cpu x86-64 -target-feature +sse %s -o - | FileCheck %s --check-prefixes=ALL,BOTH
6+
7+
! ALL-LABEL: func.func @_QPfoo()
8+
9+
! NONE-NOT: target_cpu
10+
! NONE-NOT: target_features
11+
12+
! CPU-SAME: target_cpu = "x86-64"
13+
! CPU-NOT: target_features
14+
15+
! FEATURE-NOT: target_cpu
16+
! FEATURE-SAME: target_features = #llvm.target_features<["+sse"]>
17+
18+
! BOTH-SAME: target_cpu = "x86-64"
19+
! BOTH-SAME: target_features = #llvm.target_features<["+sse"]>
20+
subroutine foo
21+
end subroutine

flang/tools/bbc/bbc.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ static mlir::LogicalResult convertFortranSourceToMLIR(
331331
auto &defKinds = semanticsContext.defaultKinds();
332332
fir::KindMapping kindMap(
333333
&ctx, llvm::ArrayRef<fir::KindTy>{fir::fromDefaultKinds(defKinds)});
334-
const llvm::DataLayout &dataLayout = targetMachine.createDataLayout();
335334
std::string targetTriple = targetMachine.getTargetTriple().normalize();
336335
// Use default lowering options for bbc.
337336
Fortran::lower::LoweringOptions loweringOptions{};
@@ -342,7 +341,7 @@ static mlir::LogicalResult convertFortranSourceToMLIR(
342341
ctx, semanticsContext, defKinds, semanticsContext.intrinsics(),
343342
semanticsContext.targetCharacteristics(), parsing.allCooked(),
344343
targetTriple, kindMap, loweringOptions, {},
345-
semanticsContext.languageFeatures(), &dataLayout);
344+
semanticsContext.languageFeatures(), targetMachine);
346345
burnside.lower(parseTree, semanticsContext);
347346
mlir::ModuleOp mlirModule = burnside.getModule();
348347
if (enableOpenMP) {

0 commit comments

Comments
 (0)