From f171e3b6caac8e74d1184b46f9b07a9c28329ec9 Mon Sep 17 00:00:00 2001 From: Thomas Symalla Date: Wed, 29 Nov 2023 14:41:00 +0100 Subject: [PATCH] Add support for typed pointers with the builtin PointerType. In some scenarios, we want to use typed pointers even if LLVM is moving to opaque pointers. Dialects currently only supports opaque pointers in its builtin. With this change, we simply return an i8* when evaluating a builtin pointer type return value or operand. This translates to an opaque pointer as well. The client that creates the operations is responsible for handling the pointers correctly (e. g. not using opaque pointers in a typed pointer scenario). Since we only test opaque pointers in llvm-dialects right now, the test context in ExampleMain is forced to use them. --- example/ExampleMain.cpp | 1 + include/llvm-dialects/Dialect/Dialect.td | 3 ++- test/example/generated/ExampleDialect.cpp.inc | 12 ++++++------ 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/example/ExampleMain.cpp b/example/ExampleMain.cpp index 91203cc..f74fd0b 100644 --- a/example/ExampleMain.cpp +++ b/example/ExampleMain.cpp @@ -253,6 +253,7 @@ int main(int argc, char **argv) { llvm::cl::ParseCommandLineOptions(argc, argv); LLVMContext context; + context.setOpaquePointers(true); auto dialectContext = DialectContext::make(context); if (g_action == Action::Build) { diff --git a/include/llvm-dialects/Dialect/Dialect.td b/include/llvm-dialects/Dialect/Dialect.td index 1674934..b425ed4 100644 --- a/include/llvm-dialects/Dialect/Dialect.td +++ b/include/llvm-dialects/Dialect/Dialect.td @@ -169,7 +169,8 @@ def I64 : TgConstant<(IntegerType 64)>, Type; def PointerType : BuiltinType { let arguments = (args type:$self, AttrI32:$address_space); - let evaluate = "::llvm::PointerType::get($_context, $address_space)"; + // Supports both typed and opaque pointers. + let evaluate = "::llvm::PointerType::get(::llvm::Type::getInt8Ty($_context), $address_space)"; let check = "$self->isPointerTy()"; let capture = ["$self->getPointerAddressSpace()"]; } diff --git a/test/example/generated/ExampleDialect.cpp.inc b/test/example/generated/ExampleDialect.cpp.inc index 67dd832..7b261ee 100644 --- a/test/example/generated/ExampleDialect.cpp.inc +++ b/test/example/generated/ExampleDialect.cpp.inc @@ -1478,9 +1478,9 @@ initial ::llvm::Type * const resultType = getResult()->getType(); (void)resultType; - if (::llvm::PointerType::get(context, 0) != ptrType) { + if (::llvm::PointerType::get(::llvm::Type::getInt8Ty(context), 0) != ptrType) { errs << " unexpected value of $ptr:\n"; - errs << " expected: " << printable(::llvm::PointerType::get(context, 0)) << '\n'; + errs << " expected: " << printable(::llvm::PointerType::get(::llvm::Type::getInt8Ty(context), 0)) << '\n'; errs << " actual: " << printable(ptrType) << '\n'; return false; @@ -1570,9 +1570,9 @@ initial ::llvm::Type * const resultType = getResult()->getType(); (void)resultType; - if (::llvm::PointerType::get(context, 0) != ptrType) { + if (::llvm::PointerType::get(::llvm::Type::getInt8Ty(context), 0) != ptrType) { errs << " unexpected value of $ptr:\n"; - errs << " expected: " << printable(::llvm::PointerType::get(context, 0)) << '\n'; + errs << " expected: " << printable(::llvm::PointerType::get(::llvm::Type::getInt8Ty(context), 0)) << '\n'; errs << " actual: " << printable(ptrType) << '\n'; return false; @@ -1662,9 +1662,9 @@ initial ::llvm::Type * const resultType = getResult()->getType(); (void)resultType; - if (::llvm::PointerType::get(context, 0) != ptrType) { + if (::llvm::PointerType::get(::llvm::Type::getInt8Ty(context), 0) != ptrType) { errs << " unexpected value of $ptr:\n"; - errs << " expected: " << printable(::llvm::PointerType::get(context, 0)) << '\n'; + errs << " expected: " << printable(::llvm::PointerType::get(::llvm::Type::getInt8Ty(context), 0)) << '\n'; errs << " actual: " << printable(ptrType) << '\n'; return false;