Skip to content

Commit

Permalink
Add support for typed pointers with the builtin PointerType.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Thomas Symalla committed Dec 1, 2023
1 parent c9d028d commit f171e3b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions example/ExampleMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ int main(int argc, char **argv) {
llvm::cl::ParseCommandLineOptions(argc, argv);

LLVMContext context;
context.setOpaquePointers(true);
auto dialectContext = DialectContext::make<xd::ExampleDialect>(context);

if (g_action == Action::Build) {
Expand Down
3 changes: 2 additions & 1 deletion include/llvm-dialects/Dialect/Dialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -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()"];
}
Expand Down
12 changes: 6 additions & 6 deletions test/example/generated/ExampleDialect.cpp.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f171e3b

Please sign in to comment.