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