MLIR 24.0.0git
LLVMInterfaces.cpp
Go to the documentation of this file.
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
13#include "mlir/IR/BuiltinOps.h"
15
16using namespace mlir;
17using namespace mlir::acc;
18
19namespace {
20struct LLVMPointerPointerLikeModel
21 : public PointerLikeType::ExternalModel<LLVMPointerPointerLikeModel,
22 LLVM::LLVMPointerType> {
23 Type getElementType(Type pointer) const { return Type(); }
24
25 mlir::Value genLoad(Type pointer, OpBuilder &builder, Location loc,
27 Type valueType) const {
28 // For LLVM pointers, we need the valueType to determine what to load
29 if (!valueType)
30 return {};
31
32 return LLVM::LoadOp::create(builder, loc, valueType, srcPtr);
33 }
34
35 bool genStore(Type pointer, OpBuilder &builder, Location loc,
36 Value valueToStore, TypedValue<PointerLikeType> destPtr) const {
37 LLVM::StoreOp::create(builder, loc, valueToStore, destPtr);
38 return true;
39 }
40
41 Value genCast(Type, OpBuilder &builder, Location loc, Value value,
42 Type resultType) const {
43 if (value.getType() == resultType)
44 return value;
45
46 auto srcPtrTy = dyn_cast<LLVM::LLVMPointerType>(value.getType());
47 auto dstPtrTy = dyn_cast<LLVM::LLVMPointerType>(resultType);
48 if (srcPtrTy && dstPtrTy) {
49 if (srcPtrTy.getAddressSpace() != dstPtrTy.getAddressSpace())
50 return LLVM::AddrSpaceCastOp::create(builder, loc, resultType, value);
51 return value;
52 }
53
54 if (srcPtrTy && isa<IntegerType>(resultType))
55 return LLVM::PtrToIntOp::create(builder, loc, resultType, value);
56
57 if (dstPtrTy) {
58 Value intVal = value;
59 if (isa<IndexType>(value.getType()))
60 intVal = arith::IndexCastUIOp::create(builder, loc,
61 builder.getI64Type(), value);
62 if (isa<IntegerType>(intVal.getType()))
63 return LLVM::IntToPtrOp::create(builder, loc, resultType, intVal);
64 }
65
66 if (auto resPtrLike = dyn_cast<PointerLikeType>(resultType))
67 if (!isa<LLVM::LLVMPointerType>(resPtrLike))
68 if (Value v = resPtrLike.genCast(builder, loc, value, resultType))
69 return v;
70 if (auto valPtrLike = dyn_cast<PointerLikeType>(value.getType()))
71 if (!isa<LLVM::LLVMPointerType>(valPtrLike))
72 if (Value v = valPtrLike.genCast(builder, loc, value, resultType))
73 return v;
74
75 return UnrealizedConversionCastOp::create(builder, loc,
76 TypeRange(resultType), value)
77 .getResult(0);
78 }
79};
80
81struct LLVMAddressOfGlobalModel
82 : public AddressOfGlobalOpInterface::ExternalModel<LLVMAddressOfGlobalModel,
83 LLVM::AddressOfOp> {
84 SymbolRefAttr getSymbol(Operation *op) const {
85 auto addressOfOp = cast<LLVM::AddressOfOp>(op);
86 return addressOfOp.getGlobalNameAttr();
87 }
88};
89} // namespace
90
92 registry.addExtension(+[](MLIRContext *ctx, OpenACCDialect *) {
93 ctx->getOrLoadDialect<LLVM::LLVMDialect>();
94 ctx->getOrLoadDialect<arith::ArithDialect>();
95 LLVM::LLVMPointerType::attachInterface<LLVMPointerPointerLikeModel>(*ctx);
96 LLVM::AddressOfOp::attachInterface<LLVMAddressOfGlobalModel>(*ctx);
97 });
98}
static Type getElementType(Type type, ArrayRef< int32_t > indices, function_ref< InFlightDiagnostic(StringRef)> emitErrorFn)
Walks the given type hierarchy with the given indices, potentially down to component granularity,...
Definition SPIRVOps.cpp:229
static void genStore(OpBuilder &builder, Location loc, Value val, Value mem, Value idx)
Generates a store with proper index typing and proper value.
static Value genLoad(OpBuilder &builder, Location loc, Value mem, Value idx)
Generates a load with proper index typing.
IntegerType getI64Type()
Definition Builders.cpp:73
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:64
T * getOrLoadDialect()
Get (or create) a dialect for the given derived dialect type.
Type getType() const
Return the type of this value.
Definition Value.h:105
void registerLLVMInterfaces(DialectRegistry &registry)
Register OpenACC interfaces for LLVM IR.
Value genCast(OpBuilder &builder, Location loc, Value value, Type dstTy)
Add type casting between arith and index types when needed.
Include the generated interface declarations.
std::conditional_t< std::is_same_v< Ty, mlir::Type >, mlir::Value, detail::TypedValue< Ty > > TypedValue
If Ty is mlir::Type this will select Value instead of having a wrapper around it.
Definition Value.h:494