@@ -202,7 +202,8 @@ class CIRAttrToValue {
202202 return llvm::TypeSwitch<mlir::Attribute, mlir::Value>(attr)
203203 .Case <cir::IntAttr, cir::FPAttr, cir::ConstComplexAttr,
204204 cir::ConstArrayAttr, cir::ConstVectorAttr, cir::ConstPtrAttr,
205- cir::ZeroAttr>([&](auto attrT) { return visitCirAttr (attrT); })
205+ cir::GlobalViewAttr, cir::ZeroAttr>(
206+ [&](auto attrT) { return visitCirAttr (attrT); })
206207 .Default ([&](auto attrT) { return mlir::Value (); });
207208 }
208209
@@ -212,6 +213,7 @@ class CIRAttrToValue {
212213 mlir::Value visitCirAttr (cir::ConstPtrAttr ptrAttr);
213214 mlir::Value visitCirAttr (cir::ConstArrayAttr attr);
214215 mlir::Value visitCirAttr (cir::ConstVectorAttr attr);
216+ mlir::Value visitCirAttr (cir::GlobalViewAttr attr);
215217 mlir::Value visitCirAttr (cir::ZeroAttr attr);
216218
217219private:
@@ -391,6 +393,62 @@ mlir::Value CIRAttrToValue::visitCirAttr(cir::ConstVectorAttr attr) {
391393 mlirValues));
392394}
393395
396+ // GlobalViewAttr visitor.
397+ mlir::Value CIRAttrToValue::visitCirAttr (cir::GlobalViewAttr globalAttr) {
398+ auto moduleOp = parentOp->getParentOfType <mlir::ModuleOp>();
399+ mlir::DataLayout dataLayout (moduleOp);
400+ mlir::Type sourceType;
401+ assert (!cir::MissingFeatures::addressSpace ());
402+ llvm::StringRef symName;
403+ mlir::Operation *sourceSymbol =
404+ mlir::SymbolTable::lookupSymbolIn (moduleOp, globalAttr.getSymbol ());
405+ if (auto llvmSymbol = dyn_cast<mlir::LLVM::GlobalOp>(sourceSymbol)) {
406+ sourceType = llvmSymbol.getType ();
407+ symName = llvmSymbol.getSymName ();
408+ } else if (auto cirSymbol = dyn_cast<cir::GlobalOp>(sourceSymbol)) {
409+ sourceType =
410+ convertTypeForMemory (*converter, dataLayout, cirSymbol.getSymType ());
411+ symName = cirSymbol.getSymName ();
412+ } else if (auto llvmFun = dyn_cast<mlir::LLVM::LLVMFuncOp>(sourceSymbol)) {
413+ sourceType = llvmFun.getFunctionType ();
414+ symName = llvmFun.getSymName ();
415+ } else if (auto fun = dyn_cast<cir::FuncOp>(sourceSymbol)) {
416+ sourceType = converter->convertType (fun.getFunctionType ());
417+ symName = fun.getSymName ();
418+ } else if (auto alias = dyn_cast<mlir::LLVM::AliasOp>(sourceSymbol)) {
419+ sourceType = alias.getType ();
420+ symName = alias.getSymName ();
421+ } else {
422+ llvm_unreachable (" Unexpected GlobalOp type" );
423+ }
424+
425+ mlir::Location loc = parentOp->getLoc ();
426+ mlir::Value addrOp = rewriter.create <mlir::LLVM::AddressOfOp>(
427+ loc, mlir::LLVM::LLVMPointerType::get (rewriter.getContext ()), symName);
428+
429+ assert (!cir::MissingFeatures::globalViewIndices ());
430+
431+ // The incubator has handling here for the attribute having integer type, but
432+ // the only test case I could find that reaches it is a direct CIR-to-LLVM IR
433+ // lowering with no clear indication of how the CIR might have been generated.
434+ // We'll hit the unreachable below if this happens.
435+ assert (!cir::MissingFeatures::globalViewIntLowering ());
436+
437+ if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(globalAttr.getType ())) {
438+ mlir::Type llvmEltTy =
439+ convertTypeForMemory (*converter, dataLayout, ptrTy.getPointee ());
440+
441+ if (llvmEltTy == sourceType)
442+ return addrOp;
443+
444+ mlir::Type llvmDstTy = converter->convertType (globalAttr.getType ());
445+ return rewriter.create <mlir::LLVM::BitcastOp>(parentOp->getLoc (), llvmDstTy,
446+ addrOp);
447+ }
448+
449+ llvm_unreachable (" Expecting pointer or integer type for GlobalViewAttr" );
450+ }
451+
394452// / ZeroAttr visitor.
395453mlir::Value CIRAttrToValue::visitCirAttr (cir::ZeroAttr attr) {
396454 mlir::Location loc = parentOp->getLoc ();
@@ -1124,7 +1182,13 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
11241182 attr = rewriter.getIntegerAttr (typeConverter->convertType (op.getType ()),
11251183 value);
11261184 } else if (mlir::isa<cir::IntType>(op.getType ())) {
1127- assert (!cir::MissingFeatures::opGlobalViewAttr ());
1185+ // Lower GlobalViewAttr to llvm.mlir.addressof + llvm.mlir.ptrtoint
1186+ if (auto ga = mlir::dyn_cast<cir::GlobalViewAttr>(op.getValue ())) {
1187+ // See the comment in visitCirAttr for why this isn't implemented.
1188+ assert (!cir::MissingFeatures::globalViewIntLowering ());
1189+ op.emitError () << " global view with integer type" ;
1190+ return mlir::failure ();
1191+ }
11281192
11291193 attr = rewriter.getIntegerAttr (
11301194 typeConverter->convertType (op.getType ()),
@@ -1142,7 +1206,12 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
11421206 return mlir::success ();
11431207 }
11441208 }
1145- assert (!cir::MissingFeatures::opGlobalViewAttr ());
1209+ // Lower GlobalViewAttr to llvm.mlir.addressof
1210+ if (auto gv = mlir::dyn_cast<cir::GlobalViewAttr>(op.getValue ())) {
1211+ auto newOp = lowerCirAttrAsValue (op, gv, rewriter, getTypeConverter ());
1212+ rewriter.replaceOp (op, newOp);
1213+ return mlir::success ();
1214+ }
11461215 attr = op.getValue ();
11471216 } else if (const auto arrTy = mlir::dyn_cast<cir::ArrayType>(op.getType ())) {
11481217 const auto constArr = mlir::dyn_cast<cir::ConstArrayAttr>(op.getValue ());
@@ -1397,8 +1466,9 @@ CIRToLLVMGlobalOpLowering::matchAndRewriteRegionInitializedGlobal(
13971466 cir::GlobalOp op, mlir::Attribute init,
13981467 mlir::ConversionPatternRewriter &rewriter) const {
13991468 // TODO: Generalize this handling when more types are needed here.
1400- assert ((isa<cir::ConstArrayAttr, cir::ConstVectorAttr, cir::ConstPtrAttr,
1401- cir::ConstComplexAttr, cir::ZeroAttr>(init)));
1469+ assert (
1470+ (isa<cir::ConstArrayAttr, cir::ConstVectorAttr, cir::ConstPtrAttr,
1471+ cir::ConstComplexAttr, cir::GlobalViewAttr, cir::ZeroAttr>(init)));
14021472
14031473 // TODO(cir): once LLVM's dialect has proper equivalent attributes this
14041474 // should be updated. For now, we use a custom op to initialize globals
@@ -1452,7 +1522,7 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
14521522 }
14531523 } else if (mlir::isa<cir::ConstArrayAttr, cir::ConstVectorAttr,
14541524 cir::ConstPtrAttr, cir::ConstComplexAttr,
1455- cir::ZeroAttr>(init.value ())) {
1525+ cir::GlobalViewAttr, cir:: ZeroAttr>(init.value ())) {
14561526 // TODO(cir): once LLVM's dialect has proper equivalent attributes this
14571527 // should be updated. For now, we use a custom op to initialize globals
14581528 // to the appropriate value.
0 commit comments