| 1 | //===- LLVMInterfaces.cpp - OpenACC external models -----------------------===// |
| 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 "mlir/Dialect/OpenACC/Extensions/LLVMInterfaces.h" |
| 10 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 11 | #include "mlir/Dialect/LLVMIR/LLVMConstantOps.h" |
| 12 | #include "mlir/Dialect/LLVMIR/LLVMConversionOps.h" |
| 13 | #include "mlir/Dialect/LLVMIR/LLVMMemoryOps.h" |
| 14 | #include "mlir/Dialect/OpenACC/OpenACC.h" |
| 15 | #include "mlir/IR/BuiltinOps.h" |
| 16 | #include "mlir/IR/DialectRegistry.h" |
| 17 | |
| 18 | using namespace mlir; |
| 19 | using namespace mlir::acc; |
| 20 | |
| 21 | namespace { |
| 22 | struct LLVMPointerPointerLikeModel |
| 23 | : public PointerLikeType::ExternalModel<LLVMPointerPointerLikeModel, |
| 24 | LLVM::LLVMPointerType> { |
| 25 | Type getElementType(Type pointer) const { return Type(); } |
| 26 | |
| 27 | mlir::Value genLoad(Type pointer, OpBuilder &builder, Location loc, |
| 28 | TypedValue<PointerLikeType> srcPtr, |
| 29 | Type valueType) const { |
| 30 | // For LLVM pointers, we need the valueType to determine what to load |
| 31 | if (!valueType) |
| 32 | return {}; |
| 33 | |
| 34 | return LLVM::LoadOp::create(builder, location: loc, type: valueType, addr: srcPtr); |
| 35 | } |
| 36 | |
| 37 | bool genStore(Type pointer, OpBuilder &builder, Location loc, |
| 38 | Value valueToStore, TypedValue<PointerLikeType> destPtr) const { |
| 39 | LLVM::StoreOp::create(builder, location: loc, value: valueToStore, addr: destPtr); |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | Value genCast(Type, OpBuilder &builder, Location loc, Value value, |
| 44 | Type resultType) const { |
| 45 | if (value.getType() == resultType) |
| 46 | return value; |
| 47 | |
| 48 | auto srcPtrTy = dyn_cast<LLVM::LLVMPointerType>(Val: value.getType()); |
| 49 | auto dstPtrTy = dyn_cast<LLVM::LLVMPointerType>(Val&: resultType); |
| 50 | if (srcPtrTy && dstPtrTy) { |
| 51 | if (srcPtrTy.getAddressSpace() != dstPtrTy.getAddressSpace()) |
| 52 | return LLVM::AddrSpaceCastOp::create(builder, location: loc, res: resultType, arg: value); |
| 53 | return value; |
| 54 | } |
| 55 | |
| 56 | if (srcPtrTy && isa<IntegerType>(Val: resultType)) |
| 57 | return LLVM::PtrToIntOp::create(builder, location: loc, res: resultType, arg: value); |
| 58 | |
| 59 | if (dstPtrTy) { |
| 60 | Value intVal = value; |
| 61 | if (isa<IndexType>(Val: value.getType())) |
| 62 | intVal = arith::IndexCastUIOp::create(builder, location: loc, |
| 63 | out: builder.getI64Type(), in: value); |
| 64 | if (isa<IntegerType>(Val: intVal.getType())) |
| 65 | return LLVM::IntToPtrOp::create(builder, location: loc, resultType, operands: intVal); |
| 66 | } |
| 67 | |
| 68 | if (auto resPtrLike = dyn_cast<PointerLikeType>(Val&: resultType)) |
| 69 | if (!isa<LLVM::LLVMPointerType>(Val: resPtrLike)) |
| 70 | if (Value v = resPtrLike.genCast(builder, loc, value, resultType)) |
| 71 | return v; |
| 72 | if (auto valPtrLike = dyn_cast<PointerLikeType>(Val: value.getType())) |
| 73 | if (!isa<LLVM::LLVMPointerType>(Val: valPtrLike)) |
| 74 | if (Value v = valPtrLike.genCast(builder, loc, value, resultType)) |
| 75 | return v; |
| 76 | |
| 77 | return UnrealizedConversionCastOp::create(builder, location: loc, |
| 78 | resultTypes: TypeRange(resultType), operands: value) |
| 79 | .getResult(i: 0); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | struct LLVMAddressOfGlobalModel |
| 84 | : public AddressOfGlobalOpInterface::ExternalModel<LLVMAddressOfGlobalModel, |
| 85 | LLVM::AddressOfOp> { |
| 86 | SymbolRefAttr getSymbol(Operation *op) const { |
| 87 | auto addressOfOp = cast<LLVM::AddressOfOp>(Val: op); |
| 88 | return addressOfOp.getGlobalNameAttr(); |
| 89 | } |
| 90 | }; |
| 91 | } // namespace |
| 92 | |
| 93 | void mlir::acc::registerLLVMInterfaces(DialectRegistry ®istry) { |
| 94 | registry.addExtension(extensionFn: +[](MLIRContext *ctx, OpenACCDialect *) { |
| 95 | ctx->getOrLoadDialect<LLVM::LLVMDialect>(); |
| 96 | ctx->getOrLoadDialect<arith::ArithDialect>(); |
| 97 | LLVM::LLVMPointerType::attachInterface<LLVMPointerPointerLikeModel>(context&: *ctx); |
| 98 | LLVM::AddressOfOp::attachInterface<LLVMAddressOfGlobalModel>(context&: *ctx); |
| 99 | }); |
| 100 | } |
| 101 | |