@@ -2422,6 +2422,178 @@ LogicalResult GlobalDtorsOp::verify() {
24222422 return success ();
24232423}
24242424
2425+ // ===----------------------------------------------------------------------===//
2426+ // Builder, printer and verifier for LLVM::GlobalOp.
2427+ // ===----------------------------------------------------------------------===//
2428+
2429+ void AliasOp::build (OpBuilder &builder, OperationState &result, Type type,
2430+ Linkage linkage, StringRef name, unsigned addrSpace,
2431+ bool dsoLocal, bool threadLocal, SymbolRefAttr comdat,
2432+ ArrayRef<NamedAttribute> attrs,
2433+ ArrayRef<Attribute> dbgExprs) {
2434+ result.addAttribute (getSymNameAttrName (result.name ),
2435+ builder.getStringAttr (name));
2436+ result.addAttribute (getAliasTypeAttrName (result.name ), TypeAttr::get (type));
2437+ if (dsoLocal)
2438+ result.addAttribute (getDsoLocalAttrName (result.name ),
2439+ builder.getUnitAttr ());
2440+ if (threadLocal)
2441+ result.addAttribute (getThreadLocal_AttrName (result.name ),
2442+ builder.getUnitAttr ());
2443+ if (comdat)
2444+ result.addAttribute (getComdatAttrName (result.name ), comdat);
2445+
2446+ result.addAttribute (getLinkageAttrName (result.name ),
2447+ LinkageAttr::get (builder.getContext (), linkage));
2448+ if (addrSpace != 0 )
2449+ result.addAttribute (getAddrSpaceAttrName (result.name ),
2450+ builder.getI32IntegerAttr (addrSpace));
2451+ result.attributes .append (attrs.begin (), attrs.end ());
2452+
2453+ result.addRegion ();
2454+ }
2455+
2456+ void AliasOp::print (OpAsmPrinter &p) {
2457+ p << ' ' << stringifyLinkage (getLinkage ()) << ' ' ;
2458+ StringRef visibility = stringifyVisibility (getVisibility_ ());
2459+ if (!visibility.empty ())
2460+ p << visibility << ' ' ;
2461+ if (getThreadLocal_ ())
2462+ p << " thread_local " ;
2463+ if (auto unnamedAddr = getUnnamedAddr ()) {
2464+ StringRef str = stringifyUnnamedAddr (*unnamedAddr);
2465+ if (!str.empty ())
2466+ p << str << ' ' ;
2467+ }
2468+ p.printSymbolName (getSymName ());
2469+ if (auto comdat = getComdat ())
2470+ p << " comdat(" << *comdat << ' )' ;
2471+
2472+ // Note that the alignment attribute is printed using the
2473+ // default syntax here, even though it is an inherent attribute
2474+ // (as defined in https://mlir.llvm.org/docs/LangRef/#attributes)
2475+ p.printOptionalAttrDict ((*this )->getAttrs (),
2476+ {SymbolTable::getSymbolAttrName (),
2477+ getAliasTypeAttrName (), getLinkageAttrName (),
2478+ getUnnamedAddrAttrName (), getThreadLocal_AttrName (),
2479+ getVisibility_AttrName (), getComdatAttrName (),
2480+ getUnnamedAddrAttrName ()});
2481+
2482+ // Print the trailing type
2483+ p << " : " << getType ();
2484+
2485+ Region &initializer = getInitializerRegion ();
2486+ if (!initializer.empty ()) {
2487+ p << ' ' ;
2488+ p.printRegion (initializer, /* printEntryBlockArgs=*/ false );
2489+ }
2490+ }
2491+
2492+ // operation ::= `llvm.mlir.alias` linkage? visibility?
2493+ // (`unnamed_addr` | `local_unnamed_addr`)?
2494+ // `thread_local`? `@` identifier
2495+ // `(` attribute? `)` (`comdat(` symbol-ref-id `)`)?
2496+ // attribute-list? (`:` type)? region
2497+ //
2498+ // The type can be omitted for string attributes, in which case it will be
2499+ // inferred from the value of the string as [strlen(value) x i8].
2500+ ParseResult AliasOp::parse (OpAsmParser &parser, OperationState &result) {
2501+ MLIRContext *ctx = parser.getContext ();
2502+ // Parse optional linkage, default to External.
2503+ result.addAttribute (getLinkageAttrName (result.name ),
2504+ LLVM::LinkageAttr::get (
2505+ ctx, parseOptionalLLVMKeyword<Linkage>(
2506+ parser, result, LLVM::Linkage::External)));
2507+
2508+ // Parse optional visibility, default to Default.
2509+ result.addAttribute (getVisibility_AttrName (result.name ),
2510+ parser.getBuilder ().getI64IntegerAttr (
2511+ parseOptionalLLVMKeyword<LLVM::Visibility, int64_t >(
2512+ parser, result, LLVM::Visibility::Default)));
2513+
2514+ // Parse optional UnnamedAddr, default to None.
2515+ result.addAttribute (getUnnamedAddrAttrName (result.name ),
2516+ parser.getBuilder ().getI64IntegerAttr (
2517+ parseOptionalLLVMKeyword<UnnamedAddr, int64_t >(
2518+ parser, result, LLVM::UnnamedAddr::None)));
2519+
2520+ if (succeeded (parser.parseOptionalKeyword (" thread_local" )))
2521+ result.addAttribute (getThreadLocal_AttrName (result.name ),
2522+ parser.getBuilder ().getUnitAttr ());
2523+
2524+ StringAttr name;
2525+ if (parser.parseSymbolName (name, getSymNameAttrName (result.name ),
2526+ result.attributes ))
2527+ return failure ();
2528+
2529+ if (succeeded (parser.parseOptionalKeyword (" comdat" ))) {
2530+ SymbolRefAttr comdat;
2531+ if (parser.parseLParen () || parser.parseAttribute (comdat) ||
2532+ parser.parseRParen ())
2533+ return failure ();
2534+
2535+ result.addAttribute (getComdatAttrName (result.name ), comdat);
2536+ }
2537+
2538+ SmallVector<Type, 1 > types;
2539+ if (parser.parseOptionalAttrDict (result.attributes ) ||
2540+ parser.parseOptionalColonTypeList (types))
2541+ return failure ();
2542+
2543+ if (types.size () > 1 )
2544+ return parser.emitError (parser.getNameLoc (), " expected zero or one type" );
2545+
2546+ Region &initRegion = *result.addRegion ();
2547+ if (parser.parseRegion (initRegion).failed ())
2548+ return failure ();
2549+
2550+ result.addAttribute (getAliasTypeAttrName (result.name ),
2551+ TypeAttr::get (types[0 ]));
2552+ return success ();
2553+ }
2554+
2555+ LogicalResult AliasOp::verify () {
2556+ bool validType = isCompatibleOuterType (getType ())
2557+ ? !llvm::isa<LLVMVoidType, LLVMTokenType,
2558+ LLVMMetadataType, LLVMLabelType>(getType ())
2559+ : llvm::isa<PointerElementTypeInterface>(getType ());
2560+ if (!validType)
2561+ return emitOpError (
2562+ " expects type to be a valid element type for an LLVM global" );
2563+ if ((*this )->getParentOp () && !satisfiesLLVMModule ((*this )->getParentOp ()))
2564+ return emitOpError (" must appear at the module level" );
2565+
2566+ if (getLinkage () == Linkage::Appending) {
2567+ if (!llvm::isa<LLVMArrayType>(getType ())) {
2568+ return emitOpError () << " expected array type for '"
2569+ << stringifyLinkage (Linkage::Appending)
2570+ << " ' linkage" ;
2571+ }
2572+ }
2573+
2574+ if (failed (verifyComdat (*this , getComdat ())))
2575+ return failure ();
2576+
2577+ return success ();
2578+ }
2579+
2580+ LogicalResult AliasOp::verifyRegions () {
2581+ if (Block *b = getInitializerBlock ()) {
2582+ ReturnOp ret = cast<ReturnOp>(b->getTerminator ());
2583+ if (ret.operand_type_begin () == ret.operand_type_end ())
2584+ return emitOpError (" initializer region cannot return void" );
2585+
2586+ for (Operation &op : *b) {
2587+ auto iface = dyn_cast<MemoryEffectOpInterface>(op);
2588+ if (!iface || !iface.hasNoEffect ())
2589+ return op.emitError ()
2590+ << " ops with side effects not allowed in aliases initializers" ;
2591+ }
2592+ }
2593+
2594+ return success ();
2595+ }
2596+
24252597// ===----------------------------------------------------------------------===//
24262598// ShuffleVectorOp
24272599// ===----------------------------------------------------------------------===//
0 commit comments