From 24eb90d3d9246791e845ce6188e02d30f1d2b28d Mon Sep 17 00:00:00 2001 From: Eric Schweitz Date: Tue, 15 Oct 2019 11:58:59 -0700 Subject: [PATCH 1/2] add methods to build identified structs --- include/mlir/Dialect/LLVMIR/LLVMDialect.h | 37 +++++++++++++++++++++++ lib/Dialect/LLVMIR/IR/LLVMDialect.cpp | 29 ++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/include/mlir/Dialect/LLVMIR/LLVMDialect.h b/include/mlir/Dialect/LLVMIR/LLVMDialect.h index e6810168bc57..5f2620209737 100644 --- a/include/mlir/Dialect/LLVMIR/LLVMDialect.h +++ b/include/mlir/Dialect/LLVMIR/LLVMDialect.h @@ -141,6 +141,43 @@ class LLVMType : public mlir::Type::TypeBase elements, + Optional name, + bool isPacked = false); + static LLVMType createStructTy(LLVMDialect *dialect, + Optional name) { + SmallVector elements; + return createStructTy(dialect, elements, name); + } + static LLVMType createStructTy(ArrayRef elements, + Optional name, + bool isPacked = false) { + assert(!elements.empty() && + "This method may not be invoked with an empty list"); + LLVMType ele0 = elements.front(); + return createStructTy(&ele0.getDialect(), elements, name, isPacked); + } + template + static typename std::enable_if_t::value, + LLVMType> + createStructTy(StringRef name, LLVMType elt1, Args... elts) { + SmallVector fields({elt1, elts...}); + Optional opt_name(name); + return createStructTy(&elt1.getDialect(), fields, opt_name); + } + static LLVMType setStructTyBody(LLVMType structType, + ArrayRef elements, + bool isPacked = false); + template + static typename std::enable_if_t::value, + LLVMType> + setStructTyBody(LLVMType structType, LLVMType elt1, Args... elts) { + SmallVector fields({elt1, elts...}); + return setStructTyBody(structType, fields); + } + private: friend LLVMDialect; diff --git a/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp index 5862efe71b51..35f48c87e5f6 100644 --- a/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp +++ b/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp @@ -1433,6 +1433,35 @@ LLVMType LLVMType::getStructTy(LLVMDialect *dialect, isPacked); }); } +inline static SmallVector +toUnderlyingTypes(ArrayRef elements) { + SmallVector llvmElements; + for (auto elt : elements) + llvmElements.push_back(elt.getUnderlyingType()); + return llvmElements; +} +LLVMType LLVMType::createStructTy(LLVMDialect *dialect, + ArrayRef elements, + Optional name, bool isPacked) { + StringRef sr = name.hasValue() ? *name : ""; + SmallVector llvmElements(toUnderlyingTypes(elements)); + return getLocked(dialect, [=] { + auto *rv = llvm::StructType::create(dialect->getLLVMContext(), sr); + if (!llvmElements.empty()) + rv->setBody(llvmElements, isPacked); + return rv; + }); +} +LLVMType LLVMType::setStructTyBody(LLVMType structType, + ArrayRef elements, bool isPacked) { + llvm::StructType *st = + llvm::cast(structType.getUnderlyingType()); + SmallVector llvmElements(toUnderlyingTypes(elements)); + return getLocked(&structType.getDialect(), [=] { + st->setBody(llvmElements, isPacked); + return st; + }); +} LLVMType LLVMType::getVectorTy(LLVMType elementType, unsigned numElements) { // Lock access to the dialect as this may modify the LLVM context. return getLocked(&elementType.getDialect(), [=] { From 78bcfed2780c59771950ca452bf1a3e44a83d096 Mon Sep 17 00:00:00 2001 From: Eric Schweitz Date: Thu, 24 Oct 2019 14:22:52 -0700 Subject: [PATCH 2/2] add unchecked stubs for recursive types in LLVM IR dialect --- test/Dialect/LLVMIR/roundtrip.mlir | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/Dialect/LLVMIR/roundtrip.mlir b/test/Dialect/LLVMIR/roundtrip.mlir index 62e2f6483b14..cfebc6c95008 100644 --- a/test/Dialect/LLVMIR/roundtrip.mlir +++ b/test/Dialect/LLVMIR/roundtrip.mlir @@ -205,3 +205,6 @@ func @null() { %1 = llvm.mlir.null : !llvm<"{void(i32, void()*)*, i64}*"> llvm.return } + +// XXXCHECK: llvm.func @recursive_type(!llvm<"%a = type { %a* }">) +//llvm.func @recursive_type(!llvm<"%a = type { %a* }">)