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 *targetFeaturesName = "fir.target_features" ; |
81 | |
82 | void fir::setTargetFeatures(mlir::ModuleOp mod, llvm::StringRef features) { |
83 | if (features.empty()) |
84 | return; |
85 | |
86 | auto *ctx = mod.getContext(); |
87 | mod->setAttr(targetFeaturesName, |
88 | mlir::LLVM::TargetFeaturesAttr::get(ctx, features)); |
89 | } |
90 | |
91 | mlir::LLVM::TargetFeaturesAttr fir::getTargetFeatures(mlir::ModuleOp mod) { |
92 | if (auto attr = mod->getAttrOfType<mlir::LLVM::TargetFeaturesAttr>( |
93 | targetFeaturesName)) |
94 | return attr; |
95 | |
96 | return {}; |
97 | } |
98 | |
99 | std::string fir::determineTargetTriple(llvm::StringRef triple) { |
100 | // Treat "" or "default" as stand-ins for the default machine. |
101 | if (triple.empty() || triple == "default" ) |
102 | return llvm::sys::getDefaultTargetTriple(); |
103 | // Treat "native" as stand-in for the host machine. |
104 | if (triple == "native" ) |
105 | return llvm::sys::getProcessTriple(); |
106 | // TODO: normalize the triple? |
107 | return triple.str(); |
108 | } |
109 | |