| 1 | //===--- LowerModule.cpp - Lower CIR Module to a Target -------------------===// |
| 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 | // This file partially mimics clang/lib/CodeGen/CodeGenModule.cpp. The queries |
| 10 | // are adapted to operate on the CIR dialect, however. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "LowerModule.h" |
| 15 | #include "CIRCXXABI.h" |
| 16 | #include "mlir/IR/BuiltinAttributes.h" |
| 17 | #include "clang/Basic/LangOptions.h" |
| 18 | #include "clang/Basic/TargetInfo.h" |
| 19 | #include "clang/Basic/TargetOptions.h" |
| 20 | #include "clang/CIR/MissingFeatures.h" |
| 21 | #include "llvm/Support/ErrorHandling.h" |
| 22 | #include "llvm/Support/FileSystem.h" |
| 23 | |
| 24 | namespace cir { |
| 25 | |
| 26 | static std::unique_ptr<CIRCXXABI> createCXXABI(LowerModule &lm) { |
| 27 | switch (lm.getCXXABIKind()) { |
| 28 | case clang::TargetCXXABI::AppleARM64: |
| 29 | case clang::TargetCXXABI::Fuchsia: |
| 30 | case clang::TargetCXXABI::GenericAArch64: |
| 31 | case clang::TargetCXXABI::GenericARM: |
| 32 | case clang::TargetCXXABI::iOS: |
| 33 | case clang::TargetCXXABI::WatchOS: |
| 34 | case clang::TargetCXXABI::GenericMIPS: |
| 35 | case clang::TargetCXXABI::GenericItanium: |
| 36 | case clang::TargetCXXABI::WebAssembly: |
| 37 | case clang::TargetCXXABI::XL: |
| 38 | return createItaniumCXXABI(lm); |
| 39 | case clang::TargetCXXABI::Microsoft: |
| 40 | return createMicrosoftCXXABI(lm); |
| 41 | } |
| 42 | |
| 43 | llvm_unreachable("invalid C++ ABI kind" ); |
| 44 | } |
| 45 | |
| 46 | LowerModule::LowerModule(clang::LangOptions langOpts, |
| 47 | clang::CodeGenOptions codeGenOpts, |
| 48 | mlir::ModuleOp &module, |
| 49 | std::unique_ptr<clang::TargetInfo> target) |
| 50 | : module(module), langOpts(std::move(langOpts)), target(std::move(target)) { |
| 51 | if (this->target) |
| 52 | abi = createCXXABI(lm&: *this); |
| 53 | } |
| 54 | |
| 55 | const TargetLoweringInfo &LowerModule::getTargetLoweringInfo() { |
| 56 | if (!targetLoweringInfo) |
| 57 | targetLoweringInfo = createTargetLoweringInfo(triple: getTarget().getTriple()); |
| 58 | return *targetLoweringInfo; |
| 59 | } |
| 60 | |
| 61 | // TODO: not to create it every time |
| 62 | std::unique_ptr<LowerModule> createLowerModule(mlir::ModuleOp module) { |
| 63 | // If the triple is not present, e.g. CIR modules parsed from text, or clang |
| 64 | // has no TargetInfo support for it yet, LowerModule currently gets created |
| 65 | // with null target and abi. If triple becomes a requirement, we should not |
| 66 | // allow LowerModules to be created with null target and abi. |
| 67 | assert(!cir::MissingFeatures::makeTripleAlwaysPresent()); |
| 68 | |
| 69 | // Fetch target information. |
| 70 | std::unique_ptr<clang::TargetInfo> targetInfo; |
| 71 | if (std::optional<llvm::Triple> triple = cir::getTripleFromModule(mod: module)) { |
| 72 | clang::TargetOptions targetOptions; |
| 73 | targetOptions.Triple = triple->str(); |
| 74 | targetInfo = clang::targets::AllocateTarget(Triple: *triple, Opts: targetOptions); |
| 75 | } |
| 76 | |
| 77 | // Populate the lowering-relevant LangOptions from the module's |
| 78 | // #cir.lowering_options attribute so a reloaded .cir lowers the same |
| 79 | // way it was compiled, without a live clang::LangOptions. When the attribute |
| 80 | // is absent (e.g. hand-written CIR) the defaults are kept; |
| 81 | clang::LangOptions langOpts; |
| 82 | if (auto loweringLangOpts = |
| 83 | mlir::dyn_cast_if_present<cir::LoweringOptionsAttr>( |
| 84 | Val: module->getAttr(name: cir::CIRDialect::getLoweringOptionsAttrName()))) { |
| 85 | langOpts.Exceptions = loweringLangOpts.getExceptions(); |
| 86 | langOpts.ThreadsafeStatics = loweringLangOpts.getThreadsafeStatics(); |
| 87 | langOpts.CUDA = loweringLangOpts.getCuda(); |
| 88 | langOpts.CUDAIsDevice = loweringLangOpts.getCudaIsDevice(); |
| 89 | langOpts.HIP = loweringLangOpts.getHip(); |
| 90 | langOpts.GPURelocatableDeviceCode = loweringLangOpts.getGpuRdc(); |
| 91 | langOpts.OpenMP = loweringLangOpts.getOpenmp(); |
| 92 | langOpts.OpenMPIsTargetDevice = loweringLangOpts.getOpenmpIsTargetDevice(); |
| 93 | langOpts.SYCLIsDevice = loweringLangOpts.getSyclIsDevice(); |
| 94 | langOpts.setClangABICompat(static_cast<clang::LangOptions::ClangABI>( |
| 95 | loweringLangOpts.getClangAbiCompat())); |
| 96 | langOpts.LongDoubleSize = loweringLangOpts.getLongDoubleSize(); |
| 97 | langOpts.RelativeCXXABIVTables = |
| 98 | loweringLangOpts.getRelativeCxxAbiVtables(); |
| 99 | } |
| 100 | // Some langopts are still not lowered. |
| 101 | assert(!cir::MissingFeatures::lowerModuleLangOpts()); |
| 102 | |
| 103 | if (auto cxxABI = module->getAttrOfType<mlir::StringAttr>( |
| 104 | name: cir::CIRDialect::getCXXABIAttrName())) |
| 105 | langOpts.CXXABI = clang::TargetCXXABI::getKind(Name: cxxABI.getValue()); |
| 106 | |
| 107 | // FIXME(cir): This just uses the default code generation options. We need to |
| 108 | // account for custom options. |
| 109 | assert(!cir::MissingFeatures::lowerModuleCodeGenOpts()); |
| 110 | clang::CodeGenOptions codeGenOpts; |
| 111 | |
| 112 | if (auto optInfo = mlir::cast_if_present<cir::OptInfoAttr>( |
| 113 | Val: module->getAttr(name: cir::CIRDialect::getOptInfoAttrName()))) { |
| 114 | codeGenOpts.OptimizationLevel = optInfo.getLevel(); |
| 115 | codeGenOpts.OptimizeSize = optInfo.getSize(); |
| 116 | } |
| 117 | |
| 118 | if (targetInfo) { |
| 119 | clang::DiagnosticOptions diagOpts; |
| 120 | clang::DiagnosticsEngine diags(clang::DiagnosticIDs::create(), diagOpts, |
| 121 | new clang::IgnoringDiagConsumer()); |
| 122 | targetInfo->adjust(Diags&: diags, Opts&: langOpts, /*Aux=*/nullptr); |
| 123 | } |
| 124 | |
| 125 | return std::make_unique<LowerModule>(args: std::move(langOpts), |
| 126 | args: std::move(codeGenOpts), args&: module, |
| 127 | args: std::move(targetInfo)); |
| 128 | } |
| 129 | |
| 130 | } // namespace cir |
| 131 | |