1c68d2895SEric Schweitz //===-- FIRContext.cpp ----------------------------------------------------===//
2c68d2895SEric Schweitz //
3c68d2895SEric Schweitz // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4c68d2895SEric Schweitz // See https://llvm.org/LICENSE.txt for license information.
5c68d2895SEric Schweitz // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c68d2895SEric Schweitz //
7c68d2895SEric Schweitz //===----------------------------------------------------------------------===//
8c68d2895SEric Schweitz //
9c68d2895SEric Schweitz // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10c68d2895SEric Schweitz //
11c68d2895SEric Schweitz //===----------------------------------------------------------------------===//
12c68d2895SEric Schweitz
13c68d2895SEric Schweitz #include "flang/Optimizer/Support/FIRContext.h"
14c68d2895SEric Schweitz #include "flang/Optimizer/Support/KindMapping.h"
15*38101b4eSAndrzej Warzynski #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
16c68d2895SEric Schweitz #include "mlir/IR/BuiltinAttributes.h"
17c68d2895SEric Schweitz #include "mlir/IR/BuiltinOps.h"
18c68d2895SEric Schweitz #include "llvm/Support/Host.h"
19c68d2895SEric Schweitz
setTargetTriple(mlir::ModuleOp mod,llvm::StringRef triple)20c68d2895SEric Schweitz void fir::setTargetTriple(mlir::ModuleOp mod, llvm::StringRef triple) {
21c68d2895SEric Schweitz auto target = fir::determineTargetTriple(triple);
22*38101b4eSAndrzej Warzynski mod->setAttr(mlir::LLVM::LLVMDialect::getTargetTripleAttrName(),
23*38101b4eSAndrzej Warzynski mlir::StringAttr::get(mod.getContext(), target));
24c68d2895SEric Schweitz }
25c68d2895SEric Schweitz
getTargetTriple(mlir::ModuleOp mod)26c68d2895SEric Schweitz llvm::Triple fir::getTargetTriple(mlir::ModuleOp mod) {
27*38101b4eSAndrzej Warzynski if (auto target = mod->getAttrOfType<mlir::StringAttr>(
28*38101b4eSAndrzej Warzynski mlir::LLVM::LLVMDialect::getTargetTripleAttrName()))
29c68d2895SEric Schweitz return llvm::Triple(target.getValue());
30c68d2895SEric Schweitz return llvm::Triple(llvm::sys::getDefaultTargetTriple());
31c68d2895SEric Schweitz }
32c68d2895SEric Schweitz
33c68d2895SEric Schweitz static constexpr const char *kindMapName = "fir.kindmap";
34c68d2895SEric Schweitz static constexpr const char *defKindName = "fir.defaultkind";
35c68d2895SEric Schweitz
setKindMapping(mlir::ModuleOp mod,fir::KindMapping & kindMap)36c68d2895SEric Schweitz void fir::setKindMapping(mlir::ModuleOp mod, fir::KindMapping &kindMap) {
371e96c4b5SValentin Clement auto *ctx = mod.getContext();
38c68d2895SEric Schweitz mod->setAttr(kindMapName, mlir::StringAttr::get(ctx, kindMap.mapToString()));
39c68d2895SEric Schweitz auto defs = kindMap.defaultsToString();
40c68d2895SEric Schweitz mod->setAttr(defKindName, mlir::StringAttr::get(ctx, defs));
41c68d2895SEric Schweitz }
42c68d2895SEric Schweitz
getKindMapping(mlir::ModuleOp mod)43c68d2895SEric Schweitz fir::KindMapping fir::getKindMapping(mlir::ModuleOp mod) {
441e96c4b5SValentin Clement auto *ctx = mod.getContext();
45c68d2895SEric Schweitz if (auto defs = mod->getAttrOfType<mlir::StringAttr>(defKindName)) {
46c68d2895SEric Schweitz auto defVals = fir::KindMapping::toDefaultKinds(defs.getValue());
47c68d2895SEric Schweitz if (auto maps = mod->getAttrOfType<mlir::StringAttr>(kindMapName))
48c68d2895SEric Schweitz return fir::KindMapping(ctx, maps.getValue(), defVals);
49c68d2895SEric Schweitz return fir::KindMapping(ctx, defVals);
50c68d2895SEric Schweitz }
51c68d2895SEric Schweitz return fir::KindMapping(ctx);
52c68d2895SEric Schweitz }
53c68d2895SEric Schweitz
determineTargetTriple(llvm::StringRef triple)54c68d2895SEric Schweitz std::string fir::determineTargetTriple(llvm::StringRef triple) {
55c68d2895SEric Schweitz // Treat "" or "default" as stand-ins for the default machine.
56c68d2895SEric Schweitz if (triple.empty() || triple == "default")
57c68d2895SEric Schweitz return llvm::sys::getDefaultTargetTriple();
58c68d2895SEric Schweitz // Treat "native" as stand-in for the host machine.
59c68d2895SEric Schweitz if (triple == "native")
60c68d2895SEric Schweitz return llvm::sys::getProcessTriple();
61c68d2895SEric Schweitz // TODO: normalize the triple?
62c68d2895SEric Schweitz return triple.str();
63c68d2895SEric Schweitz }
64