@@ -698,17 +698,25 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr,
698698 indices, true );
699699 }
700700
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 ());
705710
706- if (llvmEltTy == sourceType)
707- return addrOp;
711+ if (llvmEltTy == sourceType)
712+ return addrOp;
708713
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" );
712720}
713721
714722// / Switches on the type of attribute and calls the appropriate conversion.
@@ -1752,6 +1760,14 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
17521760 attr = rewriter.getIntegerAttr (typeConverter->convertType (op.getType ()),
17531761 value);
17541762 } 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+
17551771 attr = rewriter.getIntegerAttr (
17561772 typeConverter->convertType (op.getType ()),
17571773 mlir::cast<cir::IntAttr>(op.getValue ()).getValue ());
0 commit comments