@@ -1633,12 +1633,32 @@ static LogicalResult verify(FenceOp &op) {
1633
1633
namespace mlir {
1634
1634
namespace LLVM {
1635
1635
namespace detail {
1636
+ struct LLVMContextHandle {
1637
+ bool owned;
1638
+ llvm::LLVMContext *context;
1639
+
1640
+ LLVMContextHandle () :
1641
+ owned (true ), context(new llvm::LLVMContext()) {}
1642
+ LLVMContextHandle (llvm::LLVMContext *ctx) :
1643
+ owned (false ), context(ctx) {}
1644
+
1645
+ ~LLVMContextHandle () {
1646
+ if (owned)
1647
+ delete context;
1648
+ }
1649
+ };
1650
+
1636
1651
struct LLVMDialectImpl {
1637
- LLVMDialectImpl () : module (" LLVMDialectModule" , llvmContext) {}
1652
+ LLVMDialectImpl ()
1653
+ : module (" LLVMDialectModule" , *llvmContext.context) {}
1654
+ LLVMDialectImpl (llvm::LLVMContext *ctx)
1655
+ : llvmContext(ctx), module (" LLVMDialectModule" , *ctx) {}
1638
1656
1639
- llvm::LLVMContext llvmContext;
1657
+ LLVMContextHandle llvmContext;
1640
1658
llvm::Module module ;
1641
1659
1660
+ bool ownsContext;
1661
+
1642
1662
// / A set of LLVMTypes that are cached on construction to avoid any lookups or
1643
1663
// / locking.
1644
1664
LLVMType int1Ty, int8Ty, int16Ty, int32Ty, int64Ty, int128Ty;
@@ -1653,6 +1673,38 @@ struct LLVMDialectImpl {
1653
1673
} // end namespace LLVM
1654
1674
} // end namespace mlir
1655
1675
1676
+ LLVMDialect::LLVMDialect (MLIRContext *context, llvm::LLVMContext *llvmCtx)
1677
+ : Dialect(getDialectNamespace(), context),
1678
+ impl (new detail::LLVMDialectImpl(llvmCtx)) {
1679
+ addTypes<LLVMType>();
1680
+ addOperations<
1681
+ #define GET_OP_LIST
1682
+ #include " mlir/Dialect/LLVMIR/LLVMOps.cpp.inc"
1683
+ >();
1684
+
1685
+ // Support unknown operations because not all LLVM operations are registered.
1686
+ allowUnknownOperations ();
1687
+
1688
+ // Cache some of the common LLVM types to avoid the need for lookups/locking.
1689
+ auto &llvmContext = impl->module .getContext ();
1690
+ // / Integer Types.
1691
+ impl->int1Ty = LLVMType::get (context, llvm::Type::getInt1Ty (llvmContext));
1692
+ impl->int8Ty = LLVMType::get (context, llvm::Type::getInt8Ty (llvmContext));
1693
+ impl->int16Ty = LLVMType::get (context, llvm::Type::getInt16Ty (llvmContext));
1694
+ impl->int32Ty = LLVMType::get (context, llvm::Type::getInt32Ty (llvmContext));
1695
+ impl->int64Ty = LLVMType::get (context, llvm::Type::getInt64Ty (llvmContext));
1696
+ impl->int128Ty = LLVMType::get (context, llvm::Type::getInt128Ty (llvmContext));
1697
+ // / Float Types.
1698
+ impl->doubleTy = LLVMType::get (context, llvm::Type::getDoubleTy (llvmContext));
1699
+ impl->floatTy = LLVMType::get (context, llvm::Type::getFloatTy (llvmContext));
1700
+ impl->halfTy = LLVMType::get (context, llvm::Type::getHalfTy (llvmContext));
1701
+ impl->fp128Ty = LLVMType::get (context, llvm::Type::getFP128Ty (llvmContext));
1702
+ impl->x86_fp80Ty =
1703
+ LLVMType::get (context, llvm::Type::getX86_FP80Ty (llvmContext));
1704
+ // / Other Types.
1705
+ impl->voidTy = LLVMType::get (context, llvm::Type::getVoidTy (llvmContext));
1706
+ }
1707
+
1656
1708
LLVMDialect::LLVMDialect (MLIRContext *context)
1657
1709
: Dialect(getDialectNamespace(), context),
1658
1710
impl(new detail::LLVMDialectImpl()) {
@@ -1666,7 +1718,7 @@ LLVMDialect::LLVMDialect(MLIRContext *context)
1666
1718
allowUnknownOperations ();
1667
1719
1668
1720
// Cache some of the common LLVM types to avoid the need for lookups/locking.
1669
- auto &llvmContext = impl->llvmContext ;
1721
+ auto &llvmContext = impl->module . getContext () ;
1670
1722
// / Integer Types.
1671
1723
impl->int1Ty = LLVMType::get (context, llvm::Type::getInt1Ty (llvmContext));
1672
1724
impl->int8Ty = LLVMType::get (context, llvm::Type::getInt8Ty (llvmContext));
@@ -1690,7 +1742,7 @@ LLVMDialect::~LLVMDialect() {}
1690
1742
#define GET_OP_CLASSES
1691
1743
#include " mlir/Dialect/LLVMIR/LLVMOps.cpp.inc"
1692
1744
1693
- llvm::LLVMContext &LLVMDialect::getLLVMContext () { return impl->llvmContext ; }
1745
+ llvm::LLVMContext &LLVMDialect::getLLVMContext () { return impl->module . getContext () ; }
1694
1746
llvm::Module &LLVMDialect::getLLVMModule () { return impl->module ; }
1695
1747
1696
1748
// / Parse a type registered to this dialect.
0 commit comments