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/Support/FIRContext.h" 14 #include "flang/Optimizer/Support/KindMapping.h" 15 #include "mlir/IR/BuiltinAttributes.h" 16 #include "mlir/IR/BuiltinOps.h" 17 #include "llvm/Support/Host.h" 18 19 static constexpr const char *tripleName = "fir.triple"; 20 21 void fir::setTargetTriple(mlir::ModuleOp mod, llvm::StringRef triple) { 22 auto target = fir::determineTargetTriple(triple); 23 mod->setAttr(tripleName, mlir::StringAttr::get(mod.getContext(), target)); 24 } 25 26 llvm::Triple fir::getTargetTriple(mlir::ModuleOp mod) { 27 if (auto target = mod->getAttrOfType<mlir::StringAttr>(tripleName)) 28 return llvm::Triple(target.getValue()); 29 return llvm::Triple(llvm::sys::getDefaultTargetTriple()); 30 } 31 32 static constexpr const char *kindMapName = "fir.kindmap"; 33 static constexpr const char *defKindName = "fir.defaultkind"; 34 35 void fir::setKindMapping(mlir::ModuleOp mod, fir::KindMapping &kindMap) { 36 auto *ctx = mod.getContext(); 37 mod->setAttr(kindMapName, mlir::StringAttr::get(ctx, kindMap.mapToString())); 38 auto defs = kindMap.defaultsToString(); 39 mod->setAttr(defKindName, mlir::StringAttr::get(ctx, defs)); 40 } 41 42 fir::KindMapping fir::getKindMapping(mlir::ModuleOp mod) { 43 auto *ctx = mod.getContext(); 44 if (auto defs = mod->getAttrOfType<mlir::StringAttr>(defKindName)) { 45 auto defVals = fir::KindMapping::toDefaultKinds(defs.getValue()); 46 if (auto maps = mod->getAttrOfType<mlir::StringAttr>(kindMapName)) 47 return fir::KindMapping(ctx, maps.getValue(), defVals); 48 return fir::KindMapping(ctx, defVals); 49 } 50 return fir::KindMapping(ctx); 51 } 52 53 std::string fir::determineTargetTriple(llvm::StringRef triple) { 54 // Treat "" or "default" as stand-ins for the default machine. 55 if (triple.empty() || triple == "default") 56 return llvm::sys::getDefaultTargetTriple(); 57 // Treat "native" as stand-in for the host machine. 58 if (triple == "native") 59 return llvm::sys::getProcessTriple(); 60 // TODO: normalize the triple? 61 return triple.str(); 62 } 63