@@ -698,17 +698,25 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr,
698
698
indices, true );
699
699
}
700
700
701
- auto ptrTy = mlir::dyn_cast<cir::PointerType>(globalAttr.getType ());
702
- assert (ptrTy && " Expecting pointer type in GlobalViewAttr" );
703
- auto llvmEltTy =
704
- convertTypeForMemory (*converter, dataLayout, ptrTy.getPointee ());
701
+ if (auto intTy = mlir::dyn_cast<cir::IntType>(globalAttr.getType ())) {
702
+ auto llvmDstTy = converter->convertType (globalAttr.getType ());
703
+ return rewriter.create <mlir::LLVM::PtrToIntOp>(parentOp->getLoc (),
704
+ llvmDstTy, addrOp);
705
+ }
706
+
707
+ if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(globalAttr.getType ())) {
708
+ auto llvmEltTy =
709
+ convertTypeForMemory (*converter, dataLayout, ptrTy.getPointee ());
705
710
706
- if (llvmEltTy == sourceType)
707
- return addrOp;
711
+ if (llvmEltTy == sourceType)
712
+ return addrOp;
708
713
709
- auto llvmDstTy = converter->convertType (globalAttr.getType ());
710
- return rewriter.create <mlir::LLVM::BitcastOp>(parentOp->getLoc (), llvmDstTy,
711
- addrOp);
714
+ auto llvmDstTy = converter->convertType (globalAttr.getType ());
715
+ return rewriter.create <mlir::LLVM::BitcastOp>(parentOp->getLoc (), llvmDstTy,
716
+ addrOp);
717
+ }
718
+
719
+ llvm_unreachable (" Expecting pointer or integer type for GlobalViewAttr" );
712
720
}
713
721
714
722
// / Switches on the type of attribute and calls the appropriate conversion.
@@ -1752,6 +1760,14 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
1752
1760
attr = rewriter.getIntegerAttr (typeConverter->convertType (op.getType ()),
1753
1761
value);
1754
1762
} else if (mlir::isa<cir::IntType>(op.getType ())) {
1763
+ // Lower GlobalAddrAttr to llvm.mlir.addressof + llvm.mlir.ptrtoint
1764
+ if (auto ga = mlir::dyn_cast<cir::GlobalViewAttr>(op.getValue ())) {
1765
+ auto newOp =
1766
+ lowerCirAttrAsValue (op, ga, rewriter, getTypeConverter (), dataLayout);
1767
+ rewriter.replaceOp (op, newOp);
1768
+ return mlir::success ();
1769
+ }
1770
+
1755
1771
attr = rewriter.getIntegerAttr (
1756
1772
typeConverter->convertType (op.getType ()),
1757
1773
mlir::cast<cir::IntAttr>(op.getValue ()).getValue ());
0 commit comments