@@ -2406,6 +2406,209 @@ OpFoldResult RotateOp::fold(FoldAdaptor adaptor) {
24062406 return IntAttr::get (input.getContext (), input.getType (), resultValue);
24072407}
24082408
2409+ // ===----------------------------------------------------------------------===//
2410+ // InlineAsmOp
2411+ // ===----------------------------------------------------------------------===//
2412+
2413+ void cir::InlineAsmOp::print (OpAsmPrinter &p) {
2414+ p << ' (' << getAsmFlavor () << " , " ;
2415+ p.increaseIndent ();
2416+ p.printNewline ();
2417+
2418+ llvm::SmallVector<std::string, 3 > names{" out" , " in" , " in_out" };
2419+ auto *nameIt = names.begin ();
2420+ auto *attrIt = getOperandAttrs ().begin ();
2421+
2422+ for (auto ops : getAsmOperands ()) {
2423+ p << *nameIt << " = " ;
2424+
2425+ p << ' [' ;
2426+ llvm::interleaveComma (llvm::make_range (ops.begin (), ops.end ()), p,
2427+ [&](Value value) {
2428+ p.printOperand (value);
2429+ p << " : " << value.getType ();
2430+ if (*attrIt)
2431+ p << " (maybe_memory)" ;
2432+ attrIt++;
2433+ });
2434+ p << " ]," ;
2435+ p.printNewline ();
2436+ ++nameIt;
2437+ }
2438+
2439+ p << " {" ;
2440+ p.printString (getAsmString ());
2441+ p << " " ;
2442+ p.printString (getConstraints ());
2443+ p << " }" ;
2444+ p.decreaseIndent ();
2445+ p << ' )' ;
2446+ if (getSideEffects ())
2447+ p << " side_effects" ;
2448+
2449+ llvm::SmallVector<::llvm::StringRef, 2 > elidedAttrs;
2450+ elidedAttrs.push_back (" asm_flavor" );
2451+ elidedAttrs.push_back (" asm_string" );
2452+ elidedAttrs.push_back (" constraints" );
2453+ elidedAttrs.push_back (" operand_attrs" );
2454+ elidedAttrs.push_back (" operands_segments" );
2455+ elidedAttrs.push_back (" side_effects" );
2456+ p.printOptionalAttrDict (getOperation ()->getAttrs (), elidedAttrs);
2457+
2458+ if (auto v = getRes ())
2459+ p << " -> " << v.getType ();
2460+ }
2461+
2462+ void cir::InlineAsmOp::build (OpBuilder &odsBuilder, OperationState &odsState,
2463+ ArrayRef<ValueRange> asmOperands,
2464+ StringRef asmString, StringRef constraints,
2465+ bool sideEffects, cir::AsmFlavor asmFlavor,
2466+ ArrayRef<Attribute> operandAttrs) {
2467+ // Set up the operands_segments for VariadicOfVariadic
2468+ SmallVector<int32_t > segments;
2469+ for (auto operandRange : asmOperands) {
2470+ segments.push_back (operandRange.size ());
2471+ odsState.addOperands (operandRange);
2472+ }
2473+
2474+ odsState.addAttribute (
2475+ " operands_segments" ,
2476+ DenseI32ArrayAttr::get (odsBuilder.getContext (), segments));
2477+ odsState.addAttribute (" asm_string" , odsBuilder.getStringAttr (asmString));
2478+ odsState.addAttribute (" constraints" , odsBuilder.getStringAttr (constraints));
2479+ odsState.addAttribute (" asm_flavor" ,
2480+ AsmFlavorAttr::get (odsBuilder.getContext (), asmFlavor));
2481+
2482+ if (sideEffects)
2483+ odsState.addAttribute (" side_effects" , odsBuilder.getUnitAttr ());
2484+
2485+ odsState.addAttribute (" operand_attrs" ,
2486+ odsBuilder.getArrayAttr (operandAttrs));
2487+ }
2488+
2489+ ParseResult cir::InlineAsmOp::parse (OpAsmParser &parser,
2490+ OperationState &result) {
2491+ llvm::SmallVector<mlir::Attribute> operandAttrs;
2492+ llvm::SmallVector<int32_t > operandsGroupSizes;
2493+ std::string asmString, constraints;
2494+ Type resType;
2495+ auto *ctxt = parser.getBuilder ().getContext ();
2496+
2497+ auto error = [&](const Twine &msg) -> LogicalResult {
2498+ return parser.emitError (parser.getCurrentLocation (), msg);
2499+ };
2500+
2501+ auto expected = [&](const std::string &c) {
2502+ return error (" expected '" + c + " '" );
2503+ };
2504+
2505+ if (parser.parseLParen ().failed ())
2506+ return expected (" (" );
2507+
2508+ auto flavor = FieldParser<AsmFlavor, AsmFlavor>::parse (parser);
2509+ if (failed (flavor))
2510+ return error (" Unknown AsmFlavor" );
2511+
2512+ if (parser.parseComma ().failed ())
2513+ return expected (" ," );
2514+
2515+ auto parseValue = [&](Value &v) {
2516+ OpAsmParser::UnresolvedOperand op;
2517+
2518+ if (parser.parseOperand (op) || parser.parseColon ())
2519+ return mlir::failure ();
2520+
2521+ Type typ;
2522+ if (parser.parseType (typ).failed ())
2523+ return error (" can't parse operand type" );
2524+ llvm::SmallVector<mlir::Value> tmp;
2525+ if (parser.resolveOperand (op, typ, tmp))
2526+ return error (" can't resolve operand" );
2527+ v = tmp[0 ];
2528+ return mlir::success ();
2529+ };
2530+
2531+ auto parseOperands = [&](llvm::StringRef name) {
2532+ if (parser.parseKeyword (name).failed ())
2533+ return error (" expected " + name + " operands here" );
2534+ if (parser.parseEqual ().failed ())
2535+ return expected (" =" );
2536+ if (parser.parseLSquare ().failed ())
2537+ return expected (" [" );
2538+
2539+ int size = 0 ;
2540+ if (parser.parseOptionalRSquare ().succeeded ()) {
2541+ operandsGroupSizes.push_back (size);
2542+ if (parser.parseComma ())
2543+ return expected (" ," );
2544+ return mlir::success ();
2545+ }
2546+
2547+ if (parser.parseCommaSeparatedList ([&]() {
2548+ Value val;
2549+ if (parseValue (val).succeeded ()) {
2550+ result.operands .push_back (val);
2551+ size++;
2552+
2553+ if (parser.parseOptionalLParen ().failed ()) {
2554+ operandAttrs.push_back (mlir::Attribute ());
2555+ return mlir::success ();
2556+ }
2557+
2558+ if (parser.parseKeyword (" maybe_memory" ).succeeded ()) {
2559+ operandAttrs.push_back (mlir::UnitAttr::get (ctxt));
2560+ if (parser.parseRParen ())
2561+ return expected (" )" );
2562+ return mlir::success ();
2563+ }
2564+ }
2565+ return mlir::failure ();
2566+ }))
2567+ return mlir::failure ();
2568+
2569+ if (parser.parseRSquare ().failed () || parser.parseComma ().failed ())
2570+ return expected (" ]" );
2571+ operandsGroupSizes.push_back (size);
2572+ return mlir::success ();
2573+ };
2574+
2575+ if (parseOperands (" out" ).failed () || parseOperands (" in" ).failed () ||
2576+ parseOperands (" in_out" ).failed ())
2577+ return error (" failed to parse operands" );
2578+
2579+ if (parser.parseLBrace ())
2580+ return expected (" {" );
2581+ if (parser.parseString (&asmString))
2582+ return error (" asm string parsing failed" );
2583+ if (parser.parseString (&constraints))
2584+ return error (" constraints string parsing failed" );
2585+ if (parser.parseRBrace ())
2586+ return expected (" }" );
2587+ if (parser.parseRParen ())
2588+ return expected (" )" );
2589+
2590+ if (parser.parseOptionalKeyword (" side_effects" ).succeeded ())
2591+ result.attributes .set (" side_effects" , UnitAttr::get (ctxt));
2592+
2593+ if (parser.parseOptionalArrow ().succeeded ())
2594+ if (parser.parseType (resType).failed ())
2595+ return mlir::failure ();
2596+
2597+ if (parser.parseOptionalAttrDict (result.attributes ))
2598+ return mlir::failure ();
2599+
2600+ result.attributes .set (" asm_flavor" , AsmFlavorAttr::get (ctxt, *flavor));
2601+ result.attributes .set (" asm_string" , StringAttr::get (ctxt, asmString));
2602+ result.attributes .set (" constraints" , StringAttr::get (ctxt, constraints));
2603+ result.attributes .set (" operand_attrs" , ArrayAttr::get (ctxt, operandAttrs));
2604+ result.getOrAddProperties <InlineAsmOp::Properties>().operands_segments =
2605+ parser.getBuilder ().getDenseI32ArrayAttr (operandsGroupSizes);
2606+ if (resType)
2607+ result.addTypes (TypeRange{resType});
2608+
2609+ return mlir::success ();
2610+ }
2611+
24092612// ===----------------------------------------------------------------------===//
24102613// TableGen'd op method definitions
24112614// ===----------------------------------------------------------------------===//
0 commit comments