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