xref: /llvm-project/flang/lib/Optimizer/Dialect/Support/FIRContext.cpp (revision 839344f025fb7eff529735873f327330618b2ebb)
1b07ef9e7SRenaud-K //===-- FIRContext.cpp ----------------------------------------------------===//
2b07ef9e7SRenaud-K //
3b07ef9e7SRenaud-K // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4b07ef9e7SRenaud-K // See https://llvm.org/LICENSE.txt for license information.
5b07ef9e7SRenaud-K // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b07ef9e7SRenaud-K //
7b07ef9e7SRenaud-K //===----------------------------------------------------------------------===//
8b07ef9e7SRenaud-K //
9b07ef9e7SRenaud-K // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10b07ef9e7SRenaud-K //
11b07ef9e7SRenaud-K //===----------------------------------------------------------------------===//
12b07ef9e7SRenaud-K 
13b07ef9e7SRenaud-K #include "flang/Optimizer/Dialect/Support/FIRContext.h"
14b07ef9e7SRenaud-K #include "flang/Optimizer/Dialect/Support/KindMapping.h"
15b07ef9e7SRenaud-K #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
16b07ef9e7SRenaud-K #include "mlir/IR/BuiltinAttributes.h"
17b07ef9e7SRenaud-K #include "mlir/IR/BuiltinOps.h"
18b07ef9e7SRenaud-K #include "llvm/TargetParser/Host.h"
19b07ef9e7SRenaud-K 
20b07ef9e7SRenaud-K void fir::setTargetTriple(mlir::ModuleOp mod, llvm::StringRef triple) {
21b07ef9e7SRenaud-K   auto target = fir::determineTargetTriple(triple);
22b07ef9e7SRenaud-K   mod->setAttr(mlir::LLVM::LLVMDialect::getTargetTripleAttrName(),
23b07ef9e7SRenaud-K                mlir::StringAttr::get(mod.getContext(), target));
24b07ef9e7SRenaud-K }
25b07ef9e7SRenaud-K 
26b07ef9e7SRenaud-K llvm::Triple fir::getTargetTriple(mlir::ModuleOp mod) {
27b07ef9e7SRenaud-K   if (auto target = mod->getAttrOfType<mlir::StringAttr>(
28b07ef9e7SRenaud-K           mlir::LLVM::LLVMDialect::getTargetTripleAttrName()))
29b07ef9e7SRenaud-K     return llvm::Triple(target.getValue());
30b07ef9e7SRenaud-K   return llvm::Triple(llvm::sys::getDefaultTargetTriple());
31b07ef9e7SRenaud-K }
32b07ef9e7SRenaud-K 
33b07ef9e7SRenaud-K static constexpr const char *kindMapName = "fir.kindmap";
34b07ef9e7SRenaud-K static constexpr const char *defKindName = "fir.defaultkind";
35b07ef9e7SRenaud-K 
36b07ef9e7SRenaud-K void fir::setKindMapping(mlir::ModuleOp mod, fir::KindMapping &kindMap) {
37b07ef9e7SRenaud-K   auto *ctx = mod.getContext();
38b07ef9e7SRenaud-K   mod->setAttr(kindMapName, mlir::StringAttr::get(ctx, kindMap.mapToString()));
39b07ef9e7SRenaud-K   auto defs = kindMap.defaultsToString();
40b07ef9e7SRenaud-K   mod->setAttr(defKindName, mlir::StringAttr::get(ctx, defs));
41b07ef9e7SRenaud-K }
42b07ef9e7SRenaud-K 
43b07ef9e7SRenaud-K fir::KindMapping fir::getKindMapping(mlir::ModuleOp mod) {
44b07ef9e7SRenaud-K   auto *ctx = mod.getContext();
45b07ef9e7SRenaud-K   if (auto defs = mod->getAttrOfType<mlir::StringAttr>(defKindName)) {
46b07ef9e7SRenaud-K     auto defVals = fir::KindMapping::toDefaultKinds(defs.getValue());
47b07ef9e7SRenaud-K     if (auto maps = mod->getAttrOfType<mlir::StringAttr>(kindMapName))
48b07ef9e7SRenaud-K       return fir::KindMapping(ctx, maps.getValue(), defVals);
49b07ef9e7SRenaud-K     return fir::KindMapping(ctx, defVals);
50b07ef9e7SRenaud-K   }
51b07ef9e7SRenaud-K   return fir::KindMapping(ctx);
52b07ef9e7SRenaud-K }
53b07ef9e7SRenaud-K 
5460f02aa7SSlava Zakharin fir::KindMapping fir::getKindMapping(mlir::Operation *op) {
5560f02aa7SSlava Zakharin   auto moduleOp = mlir::dyn_cast<mlir::ModuleOp>(op);
5660f02aa7SSlava Zakharin   if (moduleOp)
5760f02aa7SSlava Zakharin     return getKindMapping(moduleOp);
5860f02aa7SSlava Zakharin 
5960f02aa7SSlava Zakharin   moduleOp = op->getParentOfType<mlir::ModuleOp>();
6060f02aa7SSlava Zakharin   return getKindMapping(moduleOp);
6160f02aa7SSlava Zakharin }
6260f02aa7SSlava Zakharin 
63837bff11SSergio Afonso static constexpr const char *targetCpuName = "fir.target_cpu";
64837bff11SSergio Afonso 
65837bff11SSergio Afonso void fir::setTargetCPU(mlir::ModuleOp mod, llvm::StringRef cpu) {
66837bff11SSergio Afonso   if (cpu.empty())
67837bff11SSergio Afonso     return;
68837bff11SSergio Afonso 
69837bff11SSergio Afonso   auto *ctx = mod.getContext();
70837bff11SSergio Afonso   mod->setAttr(targetCpuName, mlir::StringAttr::get(ctx, cpu));
71837bff11SSergio Afonso }
72837bff11SSergio Afonso 
73837bff11SSergio Afonso llvm::StringRef fir::getTargetCPU(mlir::ModuleOp mod) {
74837bff11SSergio Afonso   if (auto attr = mod->getAttrOfType<mlir::StringAttr>(targetCpuName))
75837bff11SSergio Afonso     return attr.getValue();
76837bff11SSergio Afonso 
77837bff11SSergio Afonso   return {};
78837bff11SSergio Afonso }
79837bff11SSergio Afonso 
80f1d3fe7aSAlexis Perry-Holby static constexpr const char *tuneCpuName = "fir.tune_cpu";
81f1d3fe7aSAlexis Perry-Holby 
82f1d3fe7aSAlexis Perry-Holby void fir::setTuneCPU(mlir::ModuleOp mod, llvm::StringRef cpu) {
83f1d3fe7aSAlexis Perry-Holby   if (cpu.empty())
84f1d3fe7aSAlexis Perry-Holby     return;
85f1d3fe7aSAlexis Perry-Holby 
86f1d3fe7aSAlexis Perry-Holby   auto *ctx = mod.getContext();
87f1d3fe7aSAlexis Perry-Holby 
88f1d3fe7aSAlexis Perry-Holby   mod->setAttr(tuneCpuName, mlir::StringAttr::get(ctx, cpu));
89f1d3fe7aSAlexis Perry-Holby }
90f1d3fe7aSAlexis Perry-Holby 
91f1d3fe7aSAlexis Perry-Holby llvm::StringRef fir::getTuneCPU(mlir::ModuleOp mod) {
92f1d3fe7aSAlexis Perry-Holby   if (auto attr = mod->getAttrOfType<mlir::StringAttr>(tuneCpuName))
93f1d3fe7aSAlexis Perry-Holby     return attr.getValue();
94f1d3fe7aSAlexis Perry-Holby 
95f1d3fe7aSAlexis Perry-Holby   return {};
96f1d3fe7aSAlexis Perry-Holby }
97f1d3fe7aSAlexis Perry-Holby 
98837bff11SSergio Afonso static constexpr const char *targetFeaturesName = "fir.target_features";
99837bff11SSergio Afonso 
100837bff11SSergio Afonso void fir::setTargetFeatures(mlir::ModuleOp mod, llvm::StringRef features) {
101837bff11SSergio Afonso   if (features.empty())
102837bff11SSergio Afonso     return;
103837bff11SSergio Afonso 
104837bff11SSergio Afonso   auto *ctx = mod.getContext();
105837bff11SSergio Afonso   mod->setAttr(targetFeaturesName,
106837bff11SSergio Afonso                mlir::LLVM::TargetFeaturesAttr::get(ctx, features));
107837bff11SSergio Afonso }
108837bff11SSergio Afonso 
109837bff11SSergio Afonso mlir::LLVM::TargetFeaturesAttr fir::getTargetFeatures(mlir::ModuleOp mod) {
110837bff11SSergio Afonso   if (auto attr = mod->getAttrOfType<mlir::LLVM::TargetFeaturesAttr>(
111837bff11SSergio Afonso           targetFeaturesName))
112837bff11SSergio Afonso     return attr;
113837bff11SSergio Afonso 
114837bff11SSergio Afonso   return {};
115837bff11SSergio Afonso }
116837bff11SSergio Afonso 
11790aac06cSTarun Prabhu void fir::setIdent(mlir::ModuleOp mod, llvm::StringRef ident) {
11890aac06cSTarun Prabhu   if (ident.empty())
11990aac06cSTarun Prabhu     return;
12090aac06cSTarun Prabhu 
12190aac06cSTarun Prabhu   mlir::MLIRContext *ctx = mod.getContext();
12290aac06cSTarun Prabhu   mod->setAttr(mlir::LLVM::LLVMDialect::getIdentAttrName(),
12390aac06cSTarun Prabhu                mlir::StringAttr::get(ctx, ident));
12490aac06cSTarun Prabhu }
12590aac06cSTarun Prabhu 
12690aac06cSTarun Prabhu llvm::StringRef fir::getIdent(mlir::ModuleOp mod) {
12790aac06cSTarun Prabhu   if (auto attr = mod->getAttrOfType<mlir::StringAttr>(
12890aac06cSTarun Prabhu           mlir::LLVM::LLVMDialect::getIdentAttrName()))
12990aac06cSTarun Prabhu     return attr;
13090aac06cSTarun Prabhu   return {};
13190aac06cSTarun Prabhu }
13290aac06cSTarun Prabhu 
133*839344f0STarun Prabhu void fir::setCommandline(mlir::ModuleOp mod, llvm::StringRef cmdLine) {
134*839344f0STarun Prabhu   if (cmdLine.empty())
135*839344f0STarun Prabhu     return;
136*839344f0STarun Prabhu 
137*839344f0STarun Prabhu   mlir::MLIRContext *ctx = mod.getContext();
138*839344f0STarun Prabhu   mod->setAttr(mlir::LLVM::LLVMDialect::getCommandlineAttrName(),
139*839344f0STarun Prabhu                mlir::StringAttr::get(ctx, cmdLine));
140*839344f0STarun Prabhu }
141*839344f0STarun Prabhu 
142*839344f0STarun Prabhu llvm::StringRef fir::getCommandline(mlir::ModuleOp mod) {
143*839344f0STarun Prabhu   if (auto attr = mod->getAttrOfType<mlir::StringAttr>(
144*839344f0STarun Prabhu           mlir::LLVM::LLVMDialect::getCommandlineAttrName()))
145*839344f0STarun Prabhu     return attr;
146*839344f0STarun Prabhu   return {};
147*839344f0STarun Prabhu }
148*839344f0STarun Prabhu 
149b07ef9e7SRenaud-K std::string fir::determineTargetTriple(llvm::StringRef triple) {
150b07ef9e7SRenaud-K   // Treat "" or "default" as stand-ins for the default machine.
151b07ef9e7SRenaud-K   if (triple.empty() || triple == "default")
152b07ef9e7SRenaud-K     return llvm::sys::getDefaultTargetTriple();
153b07ef9e7SRenaud-K   // Treat "native" as stand-in for the host machine.
154b07ef9e7SRenaud-K   if (triple == "native")
155b07ef9e7SRenaud-K     return llvm::sys::getProcessTriple();
156b07ef9e7SRenaud-K   // TODO: normalize the triple?
157b07ef9e7SRenaud-K   return triple.str();
158b07ef9e7SRenaud-K }
159