@@ -2276,6 +2276,8 @@ void ConvertCIRToLLVMPass::runOnOperation() {
22762276 patterns.add <CIRToLLVMCastOpLowering>(converter, patterns.getContext (), dl);
22772277 patterns.add <CIRToLLVMPtrStrideOpLowering>(converter, patterns.getContext (),
22782278 dl);
2279+ patterns.add <CIRToLLVMInlineAsmOpLowering>(converter, patterns.getContext (),
2280+ dl);
22792281 patterns.add <
22802282 // clang-format off
22812283 CIRToLLVMAssumeOpLowering,
@@ -2956,6 +2958,68 @@ mlir::LogicalResult CIRToLLVMGetBitfieldOpLowering::matchAndRewrite(
29562958 return mlir::success ();
29572959}
29582960
2961+ mlir::LogicalResult CIRToLLVMInlineAsmOpLowering::matchAndRewrite (
2962+ cir::InlineAsmOp op, OpAdaptor adaptor,
2963+ mlir::ConversionPatternRewriter &rewriter) const {
2964+ mlir::Type llResTy;
2965+ if (op.getNumResults ())
2966+ llResTy = getTypeConverter ()->convertType (op.getType (0 ));
2967+
2968+ cir::AsmFlavor dialect = op.getAsmFlavor ();
2969+ mlir::LLVM::AsmDialect llDialect = dialect == cir::AsmFlavor::x86_att
2970+ ? mlir::LLVM::AsmDialect::AD_ATT
2971+ : mlir::LLVM::AsmDialect::AD_Intel;
2972+
2973+ SmallVector<mlir::Attribute> opAttrs;
2974+ StringRef llvmAttrName = mlir::LLVM::InlineAsmOp::getElementTypeAttrName ();
2975+
2976+ // this is for the lowering to LLVM from LLVM dialect. Otherwise, if we
2977+ // don't have the result (i.e. void type as a result of operation), the
2978+ // element type attribute will be attached to the whole instruction, but not
2979+ // to the operand
2980+ if (!op.getNumResults ())
2981+ opAttrs.push_back (mlir::Attribute ());
2982+
2983+ SmallVector<mlir::Value> llvmOperands;
2984+ SmallVector<mlir::Value> cirOperands;
2985+ for (auto const &[llvmOp, cirOp] :
2986+ zip (adaptor.getAsmOperands (), op.getAsmOperands ())) {
2987+ append_range (llvmOperands, llvmOp);
2988+ append_range (cirOperands, cirOp);
2989+ }
2990+
2991+ // so far we infer the llvm dialect element type attr from
2992+ // CIR operand type.
2993+ for (auto const &[cirOpAttr, cirOp] : zip (op.getOperandAttrs (), cirOperands)) {
2994+ if (!cirOpAttr) {
2995+ opAttrs.push_back (mlir::Attribute ());
2996+ continue ;
2997+ }
2998+
2999+ llvm::SmallVector<mlir::NamedAttribute, 1 > attrs;
3000+ cir::PointerType typ =
3001+ mlir::cast<cir::PointerType>(cirOp.getType ());
3002+ mlir::TypeAttr typAttr = mlir::TypeAttr::get (convertTypeForMemory (
3003+ *getTypeConverter (), dataLayout, typ.getPointee ()));
3004+
3005+ attrs.push_back (rewriter.getNamedAttr (llvmAttrName, typAttr));
3006+ mlir::DictionaryAttr newDict = rewriter.getDictionaryAttr (attrs);
3007+ opAttrs.push_back (newDict);
3008+ }
3009+
3010+ rewriter.replaceOpWithNewOp <mlir::LLVM::InlineAsmOp>(
3011+ op, llResTy, llvmOperands, op.getAsmStringAttr (), op.getConstraintsAttr (),
3012+ op.getSideEffectsAttr (),
3013+ /* is_align_stack*/ mlir::UnitAttr (),
3014+ /* tail_call_kind*/
3015+ mlir::LLVM::TailCallKindAttr::get (
3016+ getContext (), mlir::LLVM::tailcallkind::TailCallKind::None),
3017+ mlir::LLVM::AsmDialectAttr::get (getContext (), llDialect),
3018+ rewriter.getArrayAttr (opAttrs));
3019+
3020+ return mlir::success ();
3021+ }
3022+
29593023std::unique_ptr<mlir::Pass> createConvertCIRToLLVMPass () {
29603024 return std::make_unique<ConvertCIRToLLVMPass>();
29613025}
0 commit comments