1 | //===-- FIRContext.cpp ----------------------------------------------------===// |
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 | // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "flang/Optimizer/Dialect/Support/FIRContext.h" |
14 | #include "flang/Optimizer/Dialect/Support/KindMapping.h" |
15 | #include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
16 | #include "mlir/IR/BuiltinAttributes.h" |
17 | #include "mlir/IR/BuiltinOps.h" |
18 | #include "llvm/TargetParser/Host.h" |
19 | |
20 | void fir::setTargetTriple(mlir::ModuleOp mod, llvm::StringRef triple) { |
21 | auto target = fir::determineTargetTriple(triple); |
22 | mod->setAttr(mlir::LLVM::LLVMDialect::getTargetTripleAttrName(), |
23 | mlir::StringAttr::get(mod.getContext(), target)); |
24 | } |
25 | |
26 | llvm::Triple fir::getTargetTriple(mlir::ModuleOp mod) { |
27 | if (auto target = mod->getAttrOfType<mlir::StringAttr>( |
28 | mlir::LLVM::LLVMDialect::getTargetTripleAttrName())) |
29 | return llvm::Triple(target.getValue()); |
30 | return llvm::Triple(llvm::sys::getDefaultTargetTriple()); |
31 | } |
32 | |
33 | static constexpr const char *kindMapName = "fir.kindmap" ; |
34 | static constexpr const char *defKindName = "fir.defaultkind" ; |
35 | |
36 | void fir::setKindMapping(mlir::ModuleOp mod, fir::KindMapping &kindMap) { |
37 | auto *ctx = mod.getContext(); |
38 | mod->setAttr(kindMapName, mlir::StringAttr::get(ctx, kindMap.mapToString())); |
39 | auto defs = kindMap.defaultsToString(); |
40 | mod->setAttr(defKindName, mlir::StringAttr::get(ctx, defs)); |
41 | } |
42 | |
43 | fir::KindMapping fir::getKindMapping(mlir::ModuleOp mod) { |
44 | auto *ctx = mod.getContext(); |
45 | if (auto defs = mod->getAttrOfType<mlir::StringAttr>(defKindName)) { |
46 | auto defVals = fir::KindMapping::toDefaultKinds(defs.getValue()); |
47 | if (auto maps = mod->getAttrOfType<mlir::StringAttr>(kindMapName)) |
48 | return fir::KindMapping(ctx, maps.getValue(), defVals); |
49 | return fir::KindMapping(ctx, defVals); |
50 | } |
51 | return fir::KindMapping(ctx); |
52 | } |
53 | |
54 | fir::KindMapping fir::getKindMapping(mlir::Operation *op) { |
55 | auto moduleOp = mlir::dyn_cast<mlir::ModuleOp>(op); |
56 | if (moduleOp) |
57 | return getKindMapping(moduleOp); |
58 | |
59 | moduleOp = op->getParentOfType<mlir::ModuleOp>(); |
60 | return getKindMapping(moduleOp); |
61 | } |
62 | |
63 | static constexpr const char *targetCpuName = "fir.target_cpu" ; |
64 | |
65 | void fir::setTargetCPU(mlir::ModuleOp mod, llvm::StringRef cpu) { |
66 | if (cpu.empty()) |
67 | return; |
68 | |
69 | auto *ctx = mod.getContext(); |
70 | mod->setAttr(targetCpuName, mlir::StringAttr::get(ctx, cpu)); |
71 | } |
72 | |
73 | llvm::StringRef fir::getTargetCPU(mlir::ModuleOp mod) { |
74 | if (auto attr = mod->getAttrOfType<mlir::StringAttr>(targetCpuName)) |
75 | return attr.getValue(); |
76 | |
77 | return {}; |
78 | } |
79 | |
80 | static constexpr const char *tuneCpuName = "fir.tune_cpu" ; |
81 | |
82 | void fir::setTuneCPU(mlir::ModuleOp mod, llvm::StringRef cpu) { |
83 | if (cpu.empty()) |
84 | return; |
85 | |
86 | auto *ctx = mod.getContext(); |
87 | |
88 | mod->setAttr(tuneCpuName, mlir::StringAttr::get(ctx, cpu)); |
89 | } |
90 | |
91 | llvm::StringRef fir::getTuneCPU(mlir::ModuleOp mod) { |
92 | if (auto attr = mod->getAttrOfType<mlir::StringAttr>(tuneCpuName)) |
93 | return attr.getValue(); |
94 | |
95 | return {}; |
96 | } |
97 | |
98 | static constexpr const char *targetFeaturesName = "fir.target_features" ; |
99 | |
100 | void fir::setTargetFeatures(mlir::ModuleOp mod, llvm::StringRef features) { |
101 | if (features.empty()) |
102 | return; |
103 | |
104 | auto *ctx = mod.getContext(); |
105 | mod->setAttr(targetFeaturesName, |
106 | mlir::LLVM::TargetFeaturesAttr::get(ctx, features)); |
107 | } |
108 | |
109 | mlir::LLVM::TargetFeaturesAttr fir::getTargetFeatures(mlir::ModuleOp mod) { |
110 | if (auto attr = mod->getAttrOfType<mlir::LLVM::TargetFeaturesAttr>( |
111 | targetFeaturesName)) |
112 | return attr; |
113 | |
114 | return {}; |
115 | } |
116 | |
117 | void fir::setIdent(mlir::ModuleOp mod, llvm::StringRef ident) { |
118 | if (ident.empty()) |
119 | return; |
120 | |
121 | mlir::MLIRContext *ctx = mod.getContext(); |
122 | mod->setAttr(mlir::LLVM::LLVMDialect::getIdentAttrName(), |
123 | mlir::StringAttr::get(ctx, ident)); |
124 | } |
125 | |
126 | llvm::StringRef fir::getIdent(mlir::ModuleOp mod) { |
127 | if (auto attr = mod->getAttrOfType<mlir::StringAttr>( |
128 | mlir::LLVM::LLVMDialect::getIdentAttrName())) |
129 | return attr; |
130 | return {}; |
131 | } |
132 | |
133 | void fir::setCommandline(mlir::ModuleOp mod, llvm::StringRef cmdLine) { |
134 | if (cmdLine.empty()) |
135 | return; |
136 | |
137 | mlir::MLIRContext *ctx = mod.getContext(); |
138 | mod->setAttr(mlir::LLVM::LLVMDialect::getCommandlineAttrName(), |
139 | mlir::StringAttr::get(ctx, cmdLine)); |
140 | } |
141 | |
142 | llvm::StringRef fir::getCommandline(mlir::ModuleOp mod) { |
143 | if (auto attr = mod->getAttrOfType<mlir::StringAttr>( |
144 | mlir::LLVM::LLVMDialect::getCommandlineAttrName())) |
145 | return attr; |
146 | return {}; |
147 | } |
148 | |
149 | std::string fir::determineTargetTriple(llvm::StringRef triple) { |
150 | // Treat "" or "default" as stand-ins for the default machine. |
151 | if (triple.empty() || triple == "default" ) |
152 | return llvm::sys::getDefaultTargetTriple(); |
153 | // Treat "native" as stand-in for the host machine. |
154 | if (triple == "native" ) |
155 | return llvm::sys::getProcessTriple(); |
156 | // TODO: normalize the triple? |
157 | return triple.str(); |
158 | } |
159 | |