@@ -2406,6 +2406,208 @@ 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" , odsBuilder.getArrayAttr (operandAttrs));
2486+ }
2487+
2488+ ParseResult cir::InlineAsmOp::parse (OpAsmParser &parser,
2489+ OperationState &result) {
2490+ llvm::SmallVector<mlir::Attribute> operandAttrs;
2491+ llvm::SmallVector<int32_t > operandsGroupSizes;
2492+ std::string asmString, constraints;
2493+ Type resType;
2494+ auto *ctxt = parser.getBuilder ().getContext ();
2495+
2496+ auto error = [&](const Twine &msg) -> LogicalResult {
2497+ return parser.emitError (parser.getCurrentLocation (), msg);
2498+ };
2499+
2500+ auto expected = [&](const std::string &c) {
2501+ return error (" expected '" + c + " '" );
2502+ };
2503+
2504+ if (parser.parseLParen ().failed ())
2505+ return expected (" (" );
2506+
2507+ auto flavor = FieldParser<AsmFlavor, AsmFlavor>::parse (parser);
2508+ if (failed (flavor))
2509+ return error (" Unknown AsmFlavor" );
2510+
2511+ if (parser.parseComma ().failed ())
2512+ return expected (" ," );
2513+
2514+ auto parseValue = [&](Value &v) {
2515+ OpAsmParser::UnresolvedOperand op;
2516+
2517+ if (parser.parseOperand (op) || parser.parseColon ())
2518+ return mlir::failure ();
2519+
2520+ Type typ;
2521+ if (parser.parseType (typ).failed ())
2522+ return error (" can't parse operand type" );
2523+ llvm::SmallVector<mlir::Value> tmp;
2524+ if (parser.resolveOperand (op, typ, tmp))
2525+ return error (" can't resolve operand" );
2526+ v = tmp[0 ];
2527+ return mlir::success ();
2528+ };
2529+
2530+ auto parseOperands = [&](llvm::StringRef name) {
2531+ if (parser.parseKeyword (name).failed ())
2532+ return error (" expected " + name + " operands here" );
2533+ if (parser.parseEqual ().failed ())
2534+ return expected (" =" );
2535+ if (parser.parseLSquare ().failed ())
2536+ return expected (" [" );
2537+
2538+ int size = 0 ;
2539+ if (parser.parseOptionalRSquare ().succeeded ()) {
2540+ operandsGroupSizes.push_back (size);
2541+ if (parser.parseComma ())
2542+ return expected (" ," );
2543+ return mlir::success ();
2544+ }
2545+
2546+ if (parser.parseCommaSeparatedList ([&]() {
2547+ Value val;
2548+ if (parseValue (val).succeeded ()) {
2549+ result.operands .push_back (val);
2550+ size++;
2551+
2552+ if (parser.parseOptionalLParen ().failed ()) {
2553+ operandAttrs.push_back (mlir::Attribute ());
2554+ return mlir::success ();
2555+ }
2556+
2557+ if (parser.parseKeyword (" maybe_memory" ).succeeded ()) {
2558+ operandAttrs.push_back (mlir::UnitAttr::get (ctxt));
2559+ if (parser.parseRParen ())
2560+ return expected (" )" );
2561+ return mlir::success ();
2562+ }
2563+ }
2564+ return mlir::failure ();
2565+ }))
2566+ return mlir::failure ();
2567+
2568+ if (parser.parseRSquare ().failed () || parser.parseComma ().failed ())
2569+ return expected (" ]" );
2570+ operandsGroupSizes.push_back (size);
2571+ return mlir::success ();
2572+ };
2573+
2574+ if (parseOperands (" out" ).failed () || parseOperands (" in" ).failed () ||
2575+ parseOperands (" in_out" ).failed ())
2576+ return error (" failed to parse operands" );
2577+
2578+ if (parser.parseLBrace ())
2579+ return expected (" {" );
2580+ if (parser.parseString (&asmString))
2581+ return error (" asm string parsing failed" );
2582+ if (parser.parseString (&constraints))
2583+ return error (" constraints string parsing failed" );
2584+ if (parser.parseRBrace ())
2585+ return expected (" }" );
2586+ if (parser.parseRParen ())
2587+ return expected (" )" );
2588+
2589+ if (parser.parseOptionalKeyword (" side_effects" ).succeeded ())
2590+ result.attributes .set (" side_effects" , UnitAttr::get (ctxt));
2591+
2592+ if (parser.parseOptionalArrow ().succeeded () &&
2593+ parser.parseType (resType).failed ())
2594+ return mlir::failure ();
2595+
2596+ if (parser.parseOptionalAttrDict (result.attributes ))
2597+ return mlir::failure ();
2598+
2599+ result.attributes .set (" asm_flavor" , AsmFlavorAttr::get (ctxt, *flavor));
2600+ result.attributes .set (" asm_string" , StringAttr::get (ctxt, asmString));
2601+ result.attributes .set (" constraints" , StringAttr::get (ctxt, constraints));
2602+ result.attributes .set (" operand_attrs" , ArrayAttr::get (ctxt, operandAttrs));
2603+ result.getOrAddProperties <InlineAsmOp::Properties>().operands_segments =
2604+ parser.getBuilder ().getDenseI32ArrayAttr (operandsGroupSizes);
2605+ if (resType)
2606+ result.addTypes (TypeRange{resType});
2607+
2608+ return mlir::success ();
2609+ }
2610+
24092611// ===----------------------------------------------------------------------===//
24102612// TableGen'd op method definitions
24112613// ===----------------------------------------------------------------------===//
0 commit comments