@@ -2272,6 +2272,8 @@ void ConvertCIRToLLVMPass::runOnOperation() {
22722272 patterns.add <CIRToLLVMCastOpLowering>(converter, patterns.getContext (), dl);
22732273 patterns.add <CIRToLLVMPtrStrideOpLowering>(converter, patterns.getContext (),
22742274 dl);
2275+ patterns.add <CIRToLLVMInlineAsmOpLowering>(converter, patterns.getContext (),
2276+ dl);
22752277 patterns.add <
22762278 // clang-format off
22772279 CIRToLLVMAssumeOpLowering,
@@ -2905,6 +2907,71 @@ mlir::LogicalResult CIRToLLVMGetBitfieldOpLowering::matchAndRewrite(
29052907 return mlir::success ();
29062908}
29072909
2910+ mlir::LogicalResult CIRToLLVMInlineAsmOpLowering::matchAndRewrite (
2911+ cir::InlineAsmOp op, OpAdaptor adaptor,
2912+ mlir::ConversionPatternRewriter &rewriter) const {
2913+ mlir::Type llResTy;
2914+ if (op.getNumResults ())
2915+ llResTy = getTypeConverter ()->convertType (op.getType (0 ));
2916+
2917+ auto dialect = op.getAsmFlavor ();
2918+ auto llDialect = dialect == cir::AsmFlavor::x86_att
2919+ ? mlir::LLVM::AsmDialect::AD_ATT
2920+ : mlir::LLVM::AsmDialect::AD_Intel;
2921+
2922+ SmallVector<mlir::Attribute> opAttrs;
2923+ auto llvmAttrName = mlir::LLVM::InlineAsmOp::getElementTypeAttrName ();
2924+
2925+ // this is for the lowering to LLVM from LLVM dialect. Otherwise, if we
2926+ // don't have the result (i.e. void type as a result of operation), the
2927+ // element type attribute will be attached to the whole instruction, but not
2928+ // to the operand
2929+ if (!op.getNumResults ())
2930+ opAttrs.push_back (mlir::Attribute ());
2931+
2932+ SmallVector<mlir::Value> llvmOperands;
2933+ SmallVector<mlir::Value> cirOperands;
2934+ auto llvmAsmOps = adaptor.getAsmOperands ();
2935+ auto cirAsmOps = op.getAsmOperands ();
2936+ for (size_t i = 0 ; i < llvmAsmOps.size (); ++i) {
2937+ auto llvmOps = llvmAsmOps[i];
2938+ auto cirOps = cirAsmOps[i];
2939+ llvmOperands.append (llvmOps.begin (), llvmOps.end ());
2940+ cirOperands.append (cirOps.begin (), cirOps.end ());
2941+ }
2942+
2943+ // so far we infer the llvm dialect element type attr from
2944+ // CIR operand type.
2945+ auto cirOpAttrs = op.getOperandAttrs ();
2946+ for (std::size_t i = 0 ; i < cirOpAttrs.size (); ++i) {
2947+ if (!cirOpAttrs[i]) {
2948+ opAttrs.push_back (mlir::Attribute ());
2949+ continue ;
2950+ }
2951+
2952+ SmallVector<mlir::NamedAttribute> attrs;
2953+ auto typ = cast<cir::PointerType>(cirOperands[i].getType ());
2954+ auto typAttr = mlir::TypeAttr::get (convertTypeForMemory (
2955+ *getTypeConverter (), dataLayout, typ.getPointee ()));
2956+
2957+ attrs.push_back (rewriter.getNamedAttr (llvmAttrName, typAttr));
2958+ auto newDict = rewriter.getDictionaryAttr (attrs);
2959+ opAttrs.push_back (newDict);
2960+ }
2961+
2962+ rewriter.replaceOpWithNewOp <mlir::LLVM::InlineAsmOp>(
2963+ op, llResTy, llvmOperands, op.getAsmStringAttr (), op.getConstraintsAttr (),
2964+ op.getSideEffectsAttr (),
2965+ /* is_align_stack*/ mlir::UnitAttr (),
2966+ /* tail_call_kind*/
2967+ mlir::LLVM::TailCallKindAttr::get (
2968+ getContext (), mlir::LLVM::tailcallkind::TailCallKind::None),
2969+ mlir::LLVM::AsmDialectAttr::get (getContext (), llDialect),
2970+ rewriter.getArrayAttr (opAttrs));
2971+
2972+ return mlir::success ();
2973+ }
2974+
29082975std::unique_ptr<mlir::Pass> createConvertCIRToLLVMPass () {
29092976 return std::make_unique<ConvertCIRToLLVMPass>();
29102977}
0 commit comments