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
15#include "mlir/IR/BuiltinOps.h"
17
18using namespace mlir;
19using namespace mlir::acc;
20
21namespace {
22struct 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,
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, loc, valueType, srcPtr);
35 }
36
37 bool genStore(Type pointer, OpBuilder &builder, Location loc,
38 Value valueToStore, TypedValue<PointerLikeType> destPtr) const {
39 LLVM::StoreOp::create(builder, loc, valueToStore, 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>(value.getType());
49 auto dstPtrTy = dyn_cast<LLVM::LLVMPointerType>(resultType);
50 if (srcPtrTy && dstPtrTy) {
51 if (srcPtrTy.getAddressSpace() != dstPtrTy.getAddressSpace())
52 return LLVM::AddrSpaceCastOp::create(builder, loc, resultType, value);
53 return value;
54 }
55
56 if (srcPtrTy && isa<IntegerType>(resultType))
57 return LLVM::PtrToIntOp::create(builder, loc, resultType, value);
58
59 if (dstPtrTy) {
60 Value intVal = value;
61 if (isa<IndexType>(value.getType()))
62 intVal = arith::IndexCastUIOp::create(builder, loc,
63 builder.getI64Type(), value);
64 if (isa<IntegerType>(intVal.getType()))
65 return LLVM::IntToPtrOp::create(builder, loc, resultType, intVal);
66 }
67
68 if (auto resPtrLike = dyn_cast<PointerLikeType>(resultType))
69 if (!isa<LLVM::LLVMPointerType>(resPtrLike))
70 if (Value v = resPtrLike.genCast(builder, loc, value, resultType))
71 return v;
72 if (auto valPtrLike = dyn_cast<PointerLikeType>(value.getType()))
73 if (!isa<LLVM::LLVMPointerType>(valPtrLike))
74 if (Value v = valPtrLike.genCast(builder, loc, value, resultType))
75 return v;
76
77 return UnrealizedConversionCastOp::create(builder, loc,
78 TypeRange(resultType), value)
79 .getResult(0);
80 }
81};
82
83struct LLVMAddressOfGlobalModel
84 : public AddressOfGlobalOpInterface::ExternalModel<LLVMAddressOfGlobalModel,
85 LLVM::AddressOfOp> {
86 SymbolRefAttr getSymbol(Operation *op) const {
87 auto addressOfOp = cast<LLVM::AddressOfOp>(op);
88 return addressOfOp.getGlobalNameAttr();
89 }
90};
91} // namespace
92
94 registry.addExtension(+[](MLIRContext *ctx, OpenACCDialect *) {
95 ctx->getOrLoadDialect<LLVM::LLVMDialect>();
96 ctx->getOrLoadDialect<arith::ArithDialect>();
97 LLVM::LLVMPointerType::attachInterface<LLVMPointerPointerLikeModel>(*ctx);
98 LLVM::AddressOfOp::attachInterface<LLVMAddressOfGlobalModel>(*ctx);
99 });
100}
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:230
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:71
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:496