| 1 | //===----------------------------------------------------------------------===// |
| 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 implements machinery for any CIR <-> CIR passes used by clang. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/IR/BuiltinOps.h" |
| 14 | #include "mlir/Pass/PassManager.h" |
| 15 | #include "clang/Basic/LangOptions.h" |
| 16 | #include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 17 | #include "clang/CIR/Dialect/Passes.h" |
| 18 | #include "clang/CIR/MissingFeatures.h" |
| 19 | #include "llvm/Support/TimeProfiler.h" |
| 20 | #include "llvm/TargetParser/Triple.h" |
| 21 | |
| 22 | namespace cir { |
| 23 | |
| 24 | /// Map a target triple to the ABI target that drives CallConvLowering. |
| 25 | /// Returns None for targets whose calling convention is not yet implemented. |
| 26 | static CallConvTarget getCallConvTarget(const llvm::Triple &triple) { |
| 27 | // Windows is not supported. UEFI shares its convention. |
| 28 | if (triple.getArch() == llvm::Triple::x86_64 && !triple.isOSWindowsOrUEFI()) |
| 29 | return CallConvTarget::X86_64; |
| 30 | if (triple.isAMDGPU()) |
| 31 | return CallConvTarget::AMDGPU; |
| 32 | return CallConvTarget::None; |
| 33 | } |
| 34 | |
| 35 | /// The AVX level the classifier uses to size a native vector, read from the |
| 36 | /// target ABI name. |
| 37 | static llvm::abi::X86AVXABILevel getX86AVXABILevel(llvm::StringRef abi) { |
| 38 | if (abi == "avx512" ) |
| 39 | return llvm::abi::X86AVXABILevel::AVX512; |
| 40 | if (abi == "avx" ) |
| 41 | return llvm::abi::X86AVXABILevel::AVX; |
| 42 | return llvm::abi::X86AVXABILevel::None; |
| 43 | } |
| 44 | |
| 45 | /// Whether `__attribute__((target(...)))` on a function may raise its AVX ABI |
| 46 | /// level above the command line's. A target that opts out, and any ABI older |
| 47 | /// than the rule, stay at the module level. |
| 48 | static bool allowsX86TargetAttrAvx(const llvm::Triple &triple, |
| 49 | clang::LangOptions::ClangABI compat) { |
| 50 | return !triple.isPS() && compat > clang::LangOptions::ClangABI::Ver23; |
| 51 | } |
| 52 | |
| 53 | /// The x86_64 ABI-compatibility flags, derived from the target and the |
| 54 | /// requested compatibility version. Every flag defaults to true in the ABI |
| 55 | /// library, which is not what any target computes: Clang11Compat is false for a |
| 56 | /// modern Linux target, so leaving it at the default classifies a union larger |
| 57 | /// than an eightbyte as though every member spanned its size. |
| 58 | static llvm::abi::X86ABICompatInfo |
| 59 | getX86ABICompatInfo(const llvm::Triple &triple, |
| 60 | clang::LangOptions::ClangABI compat) { |
| 61 | llvm::abi::X86ABICompatInfo abiCompat; |
| 62 | abiCompat.HonorsRevision98 = !triple.isOSDarwin(); |
| 63 | abiCompat.ClassifyIntegerMMXAsSSE = |
| 64 | compat > clang::LangOptions::ClangABI::Ver3_8 && !triple.isOSDarwin() && |
| 65 | !triple.isPS() && !triple.isOSFreeBSD(); |
| 66 | abiCompat.PassInt128VectorsInMem = |
| 67 | compat > clang::LangOptions::ClangABI::Ver9 && |
| 68 | (triple.isOSLinux() || triple.isOSNetBSD()); |
| 69 | abiCompat.ReturnCXXRecordGreaterThan128InMem = |
| 70 | compat > clang::LangOptions::ClangABI::Ver20 && !triple.isPS(); |
| 71 | abiCompat.Clang11Compat = |
| 72 | compat <= clang::LangOptions::ClangABI::Ver11 || triple.isPS(); |
| 73 | // Match the classic CodeGen default of -msmall-int-abi-compat. |
| 74 | assert(!cir::MissingFeatures::smallIntABICompatOption()); |
| 75 | abiCompat.SmallIntABICompat = triple.isOSDarwin() || triple.isPS() |
| 76 | ? llvm::abi::SmallIntABICompatKind::Clang23 |
| 77 | : llvm::abi::SmallIntABICompatKind::Both; |
| 78 | return abiCompat; |
| 79 | } |
| 80 | |
| 81 | mlir::LogicalResult |
| 82 | runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext &mlirContext, |
| 83 | bool enableVerifier, bool enableIdiomRecognizer, |
| 84 | bool enableCIRSimplify, bool enableLibOpt, |
| 85 | llvm::StringRef libOptOptions, bool enableCallConvLowering) { |
| 86 | |
| 87 | llvm::TimeTraceScope scope("CIR To CIR Passes" ); |
| 88 | |
| 89 | std::optional<llvm::Triple> triple = cir::getTripleFromModule(mod: theModule); |
| 90 | if (!triple) |
| 91 | return theModule.emitError() |
| 92 | << "missing '" << cir::CIRDialect::getTripleAttrName() |
| 93 | << "' attribute" ; |
| 94 | |
| 95 | auto abiAttr = theModule->getAttrOfType<mlir::StringAttr>( |
| 96 | name: cir::CIRDialect::getTargetABIAttrName()); |
| 97 | if (!abiAttr) |
| 98 | return theModule.emitError() |
| 99 | << "missing '" << cir::CIRDialect::getTargetABIAttrName() |
| 100 | << "' attribute" ; |
| 101 | |
| 102 | mlir::PassManager pm(&mlirContext); |
| 103 | pm.addPass(pass: mlir::createCIRCanonicalizePass()); |
| 104 | |
| 105 | if (enableCIRSimplify) |
| 106 | pm.addPass(pass: mlir::createCIRSimplifyPass()); |
| 107 | |
| 108 | if (enableIdiomRecognizer) |
| 109 | pm.addPass(pass: mlir::createIdiomRecognizerPass()); |
| 110 | |
| 111 | if (enableLibOpt) { |
| 112 | auto libOptPass = mlir::createLibOptPass(); |
| 113 | auto errorHandler = [](const llvm::Twine &) -> mlir::LogicalResult { |
| 114 | return mlir::LogicalResult::failure(); |
| 115 | }; |
| 116 | |
| 117 | if (libOptPass->initializeOptions(options: libOptOptions, errorHandler).failed()) |
| 118 | return mlir::failure(); |
| 119 | |
| 120 | pm.addPass(pass: std::move(libOptPass)); |
| 121 | } |
| 122 | |
| 123 | pm.addPass(pass: mlir::createTargetLoweringPass()); |
| 124 | pm.addPass(pass: mlir::createCXXABILoweringPass()); |
| 125 | |
| 126 | // LoweringPrepare synthesizes calls to runtime helpers such as __divsc3, and |
| 127 | // outlines dynamic global initializers into functions. It must run before |
| 128 | // CallConvLowering so the classifier sees them, otherwise their signatures |
| 129 | // go unclassified and caller and callee disagree on the ABI. |
| 130 | pm.addPass(pass: mlir::createLoweringPreparePass()); |
| 131 | |
| 132 | if (enableCallConvLowering) { |
| 133 | // CallConvLowering rewrites signatures and call sites using the classifier, |
| 134 | // so it must run after CXXABILowering has lowered C++ ABI types to plain |
| 135 | // records the classifier can handle. |
| 136 | CallConvTarget target = getCallConvTarget(triple: *triple); |
| 137 | if (target != CallConvTarget::None) { |
| 138 | // Source the ABI-compatibility version from the module's serialized |
| 139 | // #cir.lowering_options so a reloaded .cir classifies the same way |
| 140 | // it was compiled, without a live clang::LangOptions. CIRGen sets this |
| 141 | // attribute at module construction; if it is absent fall back to the |
| 142 | // LangOptions default, matching how LowerModule reads the same attribute. |
| 143 | auto compat = clang::LangOptions::ClangABI::Latest; |
| 144 | if (auto loweringLangOpts = |
| 145 | theModule->getAttrOfType<cir::LoweringOptionsAttr>( |
| 146 | name: cir::CIRDialect::getLoweringOptionsAttrName())) |
| 147 | compat = static_cast<clang::LangOptions::ClangABI>( |
| 148 | loweringLangOpts.getClangAbiCompat()); |
| 149 | pm.addPass(pass: mlir::createCallConvLoweringPass( |
| 150 | target, x86AvxAbiLevel: getX86AVXABILevel(abi: abiAttr.getValue()), |
| 151 | allowsX86TargetAttrAvx: allowsX86TargetAttrAvx(triple: *triple, compat), |
| 152 | x86AbiCompat: getX86ABICompatInfo(triple: *triple, compat))); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | pm.enableVerifier(enabled: enableVerifier); |
| 157 | (void)mlir::applyPassManagerCLOptions(pm); |
| 158 | return pm.run(op: theModule); |
| 159 | } |
| 160 | |
| 161 | } // namespace cir |
| 162 | |
| 163 | namespace mlir { |
| 164 | |
| 165 | void populateCIRPreLoweringPasses(OpPassManager &pm) { |
| 166 | pm.addPass(pass: createHoistAllocasPass()); |
| 167 | pm.addPass(pass: createCIRFlattenCFGPass()); |
| 168 | pm.addPass(pass: createCIREHABILoweringPass()); |
| 169 | pm.addPass(pass: createGotoSolverPass()); |
| 170 | pm.addPass(pass: createCIRFoldEmptyBranchesPass()); |
| 171 | } |
| 172 | |
| 173 | } // namespace mlir |
| 174 | |