@@ -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.
@@ -1681,6 +1689,14 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
1681
1689
attr = rewriter.getIntegerAttr (typeConverter->convertType (op.getType ()),
1682
1690
value);
1683
1691
} else if (mlir::isa<cir::IntType>(op.getType ())) {
1692
+ // Lower GlobalAddrAttr to llvm.mlir.addressof + llvm.mlir.ptrtoint
1693
+ if (auto ga = mlir::dyn_cast<cir::GlobalViewAttr>(op.getValue ())) {
1694
+ auto newOp =
1695
+ lowerCirAttrAsValue (op, ga, rewriter, getTypeConverter (), dataLayout);
1696
+ rewriter.replaceOp (op, newOp);
1697
+ return mlir::success ();
1698
+ }
1699
+
1684
1700
attr = rewriter.getIntegerAttr (
1685
1701
typeConverter->convertType (op.getType ()),
1686
1702
mlir::cast<cir::IntAttr>(op.getValue ()).getValue ());
0 commit comments