|
| 1 | +//===-- CUFAddConstructor.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 | +#include "flang/Optimizer/Builder/FIRBuilder.h" |
| 10 | +#include "flang/Optimizer/Dialect/CUF/CUFOps.h" |
| 11 | +#include "flang/Optimizer/Dialect/FIRAttr.h" |
| 12 | +#include "flang/Optimizer/Dialect/FIRDialect.h" |
| 13 | +#include "flang/Optimizer/Dialect/FIROpsSupport.h" |
| 14 | +#include "flang/Runtime/entry-names.h" |
| 15 | +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
| 16 | +#include "mlir/Pass/Pass.h" |
| 17 | +#include "llvm/ADT/SmallVector.h" |
| 18 | + |
| 19 | +namespace fir { |
| 20 | +#define GEN_PASS_DEF_CUFADDCONSTRUCTOR |
| 21 | +#include "flang/Optimizer/Transforms/Passes.h.inc" |
| 22 | +} // namespace fir |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +static constexpr llvm::StringRef cudaFortranCtorName{ |
| 27 | + "__cudaFortranConstructor"}; |
| 28 | + |
| 29 | +struct CUFAddConstructor |
| 30 | + : public fir::impl::CUFAddConstructorBase<CUFAddConstructor> { |
| 31 | + |
| 32 | + void runOnOperation() override { |
| 33 | + mlir::ModuleOp mod = getOperation(); |
| 34 | + mlir::OpBuilder builder{mod.getBodyRegion()}; |
| 35 | + builder.setInsertionPointToEnd(mod.getBody()); |
| 36 | + mlir::Location loc = mod.getLoc(); |
| 37 | + auto *ctx = mod.getContext(); |
| 38 | + auto voidTy = mlir::LLVM::LLVMVoidType::get(ctx); |
| 39 | + auto funcTy = |
| 40 | + mlir::LLVM::LLVMFunctionType::get(voidTy, {}, /*isVarArg=*/false); |
| 41 | + |
| 42 | + // Symbol reference to CUFRegisterAllocator. |
| 43 | + builder.setInsertionPointToEnd(mod.getBody()); |
| 44 | + auto registerFuncOp = builder.create<mlir::LLVM::LLVMFuncOp>( |
| 45 | + loc, RTNAME_STRING(CUFRegisterAllocator), funcTy); |
| 46 | + registerFuncOp.setVisibility(mlir::SymbolTable::Visibility::Private); |
| 47 | + auto cufRegisterAllocatorRef = mlir::SymbolRefAttr::get( |
| 48 | + mod.getContext(), RTNAME_STRING(CUFRegisterAllocator)); |
| 49 | + builder.setInsertionPointToEnd(mod.getBody()); |
| 50 | + |
| 51 | + // Create the constructor function that cal CUFRegisterAllocator. |
| 52 | + builder.setInsertionPointToEnd(mod.getBody()); |
| 53 | + auto func = builder.create<mlir::LLVM::LLVMFuncOp>(loc, cudaFortranCtorName, |
| 54 | + funcTy); |
| 55 | + func.setLinkage(mlir::LLVM::Linkage::Internal); |
| 56 | + builder.setInsertionPointToStart(func.addEntryBlock(builder)); |
| 57 | + builder.create<mlir::LLVM::CallOp>(loc, funcTy, cufRegisterAllocatorRef); |
| 58 | + builder.create<mlir::LLVM::ReturnOp>(loc, mlir::ValueRange{}); |
| 59 | + |
| 60 | + // Create the llvm.global_ctor with the function. |
| 61 | + // TODO: We might want to have a utility that retrieve it if already created |
| 62 | + // and adds new functions. |
| 63 | + builder.setInsertionPointToEnd(mod.getBody()); |
| 64 | + llvm::SmallVector<mlir::Attribute> funcs; |
| 65 | + funcs.push_back( |
| 66 | + mlir::FlatSymbolRefAttr::get(mod.getContext(), func.getSymName())); |
| 67 | + llvm::SmallVector<int> priorities; |
| 68 | + priorities.push_back(0); |
| 69 | + builder.create<mlir::LLVM::GlobalCtorsOp>( |
| 70 | + mod.getLoc(), builder.getArrayAttr(funcs), |
| 71 | + builder.getI32ArrayAttr(priorities)); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +} // end anonymous namespace |
0 commit comments