@@ -1877,6 +1877,18 @@ class CIRGlobalOpLowering
1877
1877
public:
1878
1878
using OpConversionPattern<mlir::cir::GlobalOp>::OpConversionPattern;
1879
1879
1880
+ // Get addrspace by converting a pointer type.
1881
+ // TODO: The approach here is a little hacky. We should access the target info
1882
+ // directly to convert the address space of global op, similar to what we do
1883
+ // for type converter.
1884
+ unsigned getGlobalOpTargetAddrSpace (mlir::cir::GlobalOp op) const {
1885
+ auto tempPtrTy = mlir::cir::PointerType::get (getContext (), op.getSymType (),
1886
+ op.getAddrSpaceAttr ());
1887
+ return cast<mlir::LLVM::LLVMPointerType>(
1888
+ typeConverter->convertType (tempPtrTy))
1889
+ .getAddressSpace ();
1890
+ }
1891
+
1880
1892
// / Replace CIR global with a region initialized LLVM global and update
1881
1893
// / insertion point to the end of the initializer block.
1882
1894
inline void setupRegionInitializedLLVMGlobalOp (
@@ -1885,7 +1897,8 @@ class CIRGlobalOpLowering
1885
1897
SmallVector<mlir::NamedAttribute> attributes;
1886
1898
auto newGlobalOp = rewriter.replaceOpWithNewOp <mlir::LLVM::GlobalOp>(
1887
1899
op, llvmType, op.getConstant (), convertLinkage (op.getLinkage ()),
1888
- op.getSymName (), nullptr , /* alignment*/ 0 , /* addrSpace*/ 0 ,
1900
+ op.getSymName (), nullptr , /* alignment*/ 0 ,
1901
+ /* addrSpace*/ getGlobalOpTargetAddrSpace (op),
1889
1902
/* dsoLocal*/ false , /* threadLocal*/ (bool )op.getTlsModelAttr (),
1890
1903
/* comdat*/ mlir::SymbolRefAttr (), attributes);
1891
1904
newGlobalOp.getRegion ().push_back (new mlir::Block ());
@@ -1915,7 +1928,7 @@ class CIRGlobalOpLowering
1915
1928
if (!init.has_value ()) {
1916
1929
rewriter.replaceOpWithNewOp <mlir::LLVM::GlobalOp>(
1917
1930
op, llvmType, isConst, linkage, symbol, mlir::Attribute (),
1918
- /* alignment*/ 0 , /* addrSpace*/ 0 ,
1931
+ /* alignment*/ 0 , /* addrSpace*/ getGlobalOpTargetAddrSpace (op) ,
1919
1932
/* dsoLocal*/ isDsoLocal, /* threadLocal*/ (bool )op.getTlsModelAttr (),
1920
1933
/* comdat*/ mlir::SymbolRefAttr (), attributes);
1921
1934
return mlir::success ();
@@ -2003,7 +2016,7 @@ class CIRGlobalOpLowering
2003
2016
// Rewrite op.
2004
2017
auto llvmGlobalOp = rewriter.replaceOpWithNewOp <mlir::LLVM::GlobalOp>(
2005
2018
op, llvmType, isConst, linkage, symbol, init.value (),
2006
- /* alignment*/ 0 , /* addrSpace*/ 0 ,
2019
+ /* alignment*/ 0 , /* addrSpace*/ getGlobalOpTargetAddrSpace (op) ,
2007
2020
/* dsoLocal*/ false , /* threadLocal*/ (bool )op.getTlsModelAttr (),
2008
2021
/* comdat*/ mlir::SymbolRefAttr (), attributes);
2009
2022
auto mod = op->getParentOfType <mlir::ModuleOp>();
0 commit comments