| 1 | //===- ACCDeclareCtorDtorConversion.cpp - Declare ctor/dtor to LLVM -------===// |
| 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 | // Convert ACC declare global constructors and destructors to LLVM functions |
| 10 | // registered in llvm.mlir.global_ctors / llvm.mlir.global_dtors. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Dialect/LLVMIR/LLVMCallOps.h" |
| 15 | #include "mlir/Dialect/LLVMIR/LLVMModuleOps.h" |
| 16 | #include "mlir/Dialect/LLVMIR/LLVMTerminatorOps.h" |
| 17 | #include "mlir/Dialect/OpenACC/OpenACC.h" |
| 18 | #include "mlir/Dialect/OpenACC/Transforms/Passes.h" |
| 19 | #include "mlir/IR/Builders.h" |
| 20 | #include "mlir/IR/IRMapping.h" |
| 21 | #include "mlir/Pass/Pass.h" |
| 22 | |
| 23 | namespace mlir { |
| 24 | namespace acc { |
| 25 | #define GEN_PASS_DEF_ACCDECLARECTORDTORCONVERSION |
| 26 | #include "mlir/Dialect/OpenACC/Transforms/Passes.h.inc" |
| 27 | } // namespace acc |
| 28 | } // namespace mlir |
| 29 | |
| 30 | using namespace mlir; |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | static void collectExistingGlobalCtors( |
| 35 | ModuleOp mod, SmallVectorImpl<Attribute> &ctors, |
| 36 | SmallVectorImpl<int32_t> &priorities, SmallVectorImpl<Attribute> &data, |
| 37 | SmallVectorImpl<LLVM::GlobalCtorsOp> &globalCtorsOps) { |
| 38 | for (auto globalCtors : mod.getOps<LLVM::GlobalCtorsOp>()) { |
| 39 | ctors.append(in_start: globalCtors.getCtors().begin(), in_end: globalCtors.getCtors().end()); |
| 40 | for (Attribute attr : globalCtors.getPriorities()) |
| 41 | priorities.push_back(Elt: cast<IntegerAttr>(Val&: attr).getInt()); |
| 42 | data.append(in_start: globalCtors.getData().begin(), in_end: globalCtors.getData().end()); |
| 43 | globalCtorsOps.push_back(Elt: globalCtors); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | static void collectExistingGlobalDtors( |
| 48 | ModuleOp mod, SmallVectorImpl<Attribute> &dtors, |
| 49 | SmallVectorImpl<int32_t> &priorities, SmallVectorImpl<Attribute> &data, |
| 50 | SmallVectorImpl<LLVM::GlobalDtorsOp> &globalDtorsOps) { |
| 51 | for (auto globalDtors : mod.getOps<LLVM::GlobalDtorsOp>()) { |
| 52 | dtors.append(in_start: globalDtors.getDtors().begin(), in_end: globalDtors.getDtors().end()); |
| 53 | for (Attribute attr : globalDtors.getPriorities()) |
| 54 | priorities.push_back(Elt: cast<IntegerAttr>(Val&: attr).getInt()); |
| 55 | data.append(in_start: globalDtors.getData().begin(), in_end: globalDtors.getData().end()); |
| 56 | globalDtorsOps.push_back(Elt: globalDtors); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static void replaceGlobalCtors(ModuleOp mod, OpBuilder &builder, |
| 61 | ArrayRef<Attribute> ctors, |
| 62 | ArrayRef<int32_t> priorities, |
| 63 | ArrayRef<Attribute> data, |
| 64 | MutableArrayRef<LLVM::GlobalCtorsOp> oldOps) { |
| 65 | for (auto globalCtors : oldOps) |
| 66 | globalCtors.erase(); |
| 67 | if (ctors.empty()) |
| 68 | return; |
| 69 | |
| 70 | builder.setInsertionPointToEnd(mod.getBody()); |
| 71 | LLVM::GlobalCtorsOp::create( |
| 72 | builder, location: mod.getLoc(), ctors: builder.getArrayAttr(value: ctors), |
| 73 | priorities: builder.getI32ArrayAttr(values: priorities), data: builder.getArrayAttr(value: data)); |
| 74 | } |
| 75 | |
| 76 | static void replaceGlobalDtors(ModuleOp mod, OpBuilder &builder, |
| 77 | ArrayRef<Attribute> dtors, |
| 78 | ArrayRef<int32_t> priorities, |
| 79 | ArrayRef<Attribute> data, |
| 80 | MutableArrayRef<LLVM::GlobalDtorsOp> oldOps) { |
| 81 | for (auto globalDtors : oldOps) |
| 82 | globalDtors.erase(); |
| 83 | if (dtors.empty()) |
| 84 | return; |
| 85 | |
| 86 | builder.setInsertionPointToEnd(mod.getBody()); |
| 87 | LLVM::GlobalDtorsOp::create( |
| 88 | builder, location: mod.getLoc(), dtors: builder.getArrayAttr(value: dtors), |
| 89 | priorities: builder.getI32ArrayAttr(values: priorities), data: builder.getArrayAttr(value: data)); |
| 90 | } |
| 91 | |
| 92 | /// Create an llvm.func from an acc.global_ctor / acc.global_dtor region. |
| 93 | /// Nested operations are cloned unchanged for later lowering. |
| 94 | static LLVM::LLVMFuncOp createLLVMFunctionFromRegion(StringRef symName, |
| 95 | Region ®ion, |
| 96 | ModuleOp mod, |
| 97 | OpBuilder &builder) { |
| 98 | auto llvmVoidTy = LLVM::LLVMVoidType::get(ctx: mod.getContext()); |
| 99 | auto funcTy = LLVM::LLVMFunctionType::get(result: llvmVoidTy, arguments: {}, /*isVarArg=*/false); |
| 100 | builder.setInsertionPointToEnd(mod.getBody()); |
| 101 | auto newFunc = LLVM::LLVMFuncOp::create(builder, location: mod.getLoc(), name: symName, |
| 102 | type: funcTy, linkage: LLVM::Linkage::Internal); |
| 103 | |
| 104 | Block *entry = newFunc.addEntryBlock(builder); |
| 105 | builder.setInsertionPointToStart(entry); |
| 106 | |
| 107 | IRMapping mapping; |
| 108 | mapping.map(from: region.front().getArguments(), to: entry->getArguments()); |
| 109 | |
| 110 | for (Operation &op : region.front()) { |
| 111 | Operation *clonedOp = builder.clone(op, mapper&: mapping); |
| 112 | mapping.map(from: op.getResults(), to: clonedOp->getResults()); |
| 113 | } |
| 114 | |
| 115 | Operation *accTerm = entry->getTerminator(); |
| 116 | LLVM::ReturnOp::create(builder, location: mod.getLoc(), args: ValueRange{}); |
| 117 | accTerm->erase(); |
| 118 | |
| 119 | return newFunc; |
| 120 | } |
| 121 | |
| 122 | /// Declare extra runtime functions and call them from a defined llvm.func |
| 123 | /// registered in llvm.mlir.global_ctors. The extra functions themselves stay |
| 124 | /// declarations: llvm.mlir.global_ctors requires a function with a body. |
| 125 | static LLVM::LLVMFuncOp |
| 126 | (ModuleOp mod, OpBuilder &builder, |
| 127 | ArrayRef<std::string> , |
| 128 | StringRef ) { |
| 129 | auto llvmVoidTy = LLVM::LLVMVoidType::get(ctx: mod.getContext()); |
| 130 | auto funcTy = LLVM::LLVMFunctionType::get(result: llvmVoidTy, arguments: {}, /*isVarArg=*/false); |
| 131 | |
| 132 | builder.setInsertionPointToEnd(mod.getBody()); |
| 133 | for (const std::string &name : extraNames) { |
| 134 | if (mod.lookupSymbol<LLVM::LLVMFuncOp>(name)) |
| 135 | continue; |
| 136 | LLVM::LLVMFuncOp::create(builder, location: mod.getLoc(), name, type: funcTy, |
| 137 | linkage: LLVM::Linkage::External); |
| 138 | } |
| 139 | |
| 140 | auto wrapper = LLVM::LLVMFuncOp::create(builder, location: mod.getLoc(), name: extraCtorName, |
| 141 | type: funcTy, linkage: LLVM::Linkage::Internal); |
| 142 | Block *entry = wrapper.addEntryBlock(builder); |
| 143 | builder.setInsertionPointToStart(entry); |
| 144 | for (const std::string &name : extraNames) |
| 145 | LLVM::CallOp::create(builder, location: mod.getLoc(), calleeType: funcTy, |
| 146 | callee: FlatSymbolRefAttr::get(ctx: mod.getContext(), value: name)); |
| 147 | LLVM::ReturnOp::create(builder, location: mod.getLoc(), args: ValueRange{}); |
| 148 | return wrapper; |
| 149 | } |
| 150 | |
| 151 | struct ACCDeclareCtorDtorConversion |
| 152 | : public acc::impl::ACCDeclareCtorDtorConversionBase< |
| 153 | ACCDeclareCtorDtorConversion> { |
| 154 | using Base::Base; |
| 155 | |
| 156 | void runOnOperation() override { |
| 157 | ModuleOp mod = getOperation(); |
| 158 | OpBuilder builder{mod.getBodyRegion()}; |
| 159 | SmallVector<Operation *> worklist; |
| 160 | |
| 161 | SmallVector<Attribute, 8> allCtors; |
| 162 | SmallVector<int32_t, 8> ctorPriorities; |
| 163 | SmallVector<Attribute, 8> ctorData; |
| 164 | SmallVector<LLVM::GlobalCtorsOp, 4> globalCtorsOps; |
| 165 | collectExistingGlobalCtors(mod, ctors&: allCtors, priorities&: ctorPriorities, data&: ctorData, |
| 166 | globalCtorsOps); |
| 167 | size_t existingCtorCount = allCtors.size(); |
| 168 | |
| 169 | SmallVector<Attribute, 8> allDtors; |
| 170 | SmallVector<int32_t, 8> dtorPriorities; |
| 171 | SmallVector<Attribute, 8> dtorData; |
| 172 | SmallVector<LLVM::GlobalDtorsOp, 4> globalDtorsOps; |
| 173 | collectExistingGlobalDtors(mod, dtors&: allDtors, priorities&: dtorPriorities, data&: dtorData, |
| 174 | globalDtorsOps); |
| 175 | size_t existingDtorCount = allDtors.size(); |
| 176 | |
| 177 | mod.walk(callback: [&](acc::GlobalConstructorOp op) { |
| 178 | LLVM::LLVMFuncOp newCtor = createLLVMFunctionFromRegion( |
| 179 | symName: op.getSymName(), region&: op.getRegion(), mod, builder); |
| 180 | allCtors.push_back( |
| 181 | Elt: FlatSymbolRefAttr::get(ctx: mod.getContext(), value: newCtor.getSymName())); |
| 182 | ctorPriorities.push_back(Elt: priority); |
| 183 | // Null associated data: constructor always runs at load time. |
| 184 | ctorData.push_back(Elt: LLVM::ZeroAttr::get(ctx: builder.getContext())); |
| 185 | worklist.push_back(Elt: op.getOperation()); |
| 186 | }); |
| 187 | |
| 188 | mod.walk(callback: [&](acc::GlobalDestructorOp op) { |
| 189 | if (generateDtors) { |
| 190 | LLVM::LLVMFuncOp newDtor = createLLVMFunctionFromRegion( |
| 191 | symName: op.getSymName(), region&: op.getRegion(), mod, builder); |
| 192 | allDtors.push_back( |
| 193 | Elt: FlatSymbolRefAttr::get(ctx: mod.getContext(), value: newDtor.getSymName())); |
| 194 | dtorPriorities.push_back(Elt: priority); |
| 195 | // Null associated data: destructor always runs at unload time. |
| 196 | dtorData.push_back(Elt: LLVM::ZeroAttr::get(ctx: builder.getContext())); |
| 197 | } |
| 198 | worklist.push_back(Elt: op.getOperation()); |
| 199 | }); |
| 200 | |
| 201 | bool hasEntryPoint = !entryPointName.empty() && |
| 202 | static_cast<bool>(mod.lookupSymbol(name: entryPointName)); |
| 203 | SmallVector<std::string, 4> ; |
| 204 | for (const auto &funcName : extraConstructors) |
| 205 | extraNames.push_back(Elt: funcName); |
| 206 | for (const auto &funcName : entryOnlyConstructors) { |
| 207 | if (hasEntryPoint) |
| 208 | extraNames.push_back(Elt: funcName); |
| 209 | } |
| 210 | if (!extraNames.empty()) { |
| 211 | LLVM::LLVMFuncOp = |
| 212 | createExtraConstructorCaller(mod, builder, extraNames, extraCtorName); |
| 213 | allCtors.push_back( |
| 214 | Elt: FlatSymbolRefAttr::get(ctx: mod.getContext(), value: extraCtor.getSymName())); |
| 215 | ctorPriorities.push_back(Elt: priority); |
| 216 | ctorData.push_back(Elt: LLVM::ZeroAttr::get(ctx: builder.getContext())); |
| 217 | } |
| 218 | |
| 219 | if (allCtors.size() > existingCtorCount) |
| 220 | replaceGlobalCtors(mod, builder, ctors: allCtors, priorities: ctorPriorities, data: ctorData, |
| 221 | oldOps: globalCtorsOps); |
| 222 | if (allDtors.size() > existingDtorCount) |
| 223 | replaceGlobalDtors(mod, builder, dtors: allDtors, priorities: dtorPriorities, data: dtorData, |
| 224 | oldOps: globalDtorsOps); |
| 225 | |
| 226 | for (Operation *op : worklist) |
| 227 | op->erase(); |
| 228 | } |
| 229 | }; |
| 230 | |
| 231 | } // namespace |
| 232 | |