4949#include " llvm/ADT/Sequence.h"
5050#include " llvm/ADT/SmallVector.h"
5151#include " llvm/Support/Casting.h"
52+ #include " llvm/Support/ErrorHandling.h"
5253#include < optional>
5354
5455using namespace cir ;
@@ -534,6 +535,66 @@ class CIRConstantLowering
534535 }
535536};
536537
538+ class CIRVAStartLowering
539+ : public mlir::OpConversionPattern<mlir::cir::VAStartOp> {
540+ public:
541+ using OpConversionPattern<mlir::cir::VAStartOp>::OpConversionPattern;
542+
543+ mlir::LogicalResult
544+ matchAndRewrite (mlir::cir::VAStartOp op, OpAdaptor adaptor,
545+ mlir::ConversionPatternRewriter &rewriter) const override {
546+ auto i8PtrTy = mlir::LLVM::LLVMPointerType::get (rewriter.getI8Type ());
547+ auto vaList = rewriter.create <mlir::LLVM::BitcastOp>(
548+ op.getLoc (), i8PtrTy, adaptor.getOperands ().front ());
549+ rewriter.replaceOpWithNewOp <mlir::LLVM::VaStartOp>(op, vaList);
550+ return mlir::success ();
551+ }
552+ };
553+
554+ class CIRVAEndLowering : public mlir ::OpConversionPattern<mlir::cir::VAEndOp> {
555+ public:
556+ using OpConversionPattern<mlir::cir::VAEndOp>::OpConversionPattern;
557+
558+ mlir::LogicalResult
559+ matchAndRewrite (mlir::cir::VAEndOp op, OpAdaptor adaptor,
560+ mlir::ConversionPatternRewriter &rewriter) const override {
561+ auto i8PtrTy = mlir::LLVM::LLVMPointerType::get (rewriter.getI8Type ());
562+ auto vaList = rewriter.create <mlir::LLVM::BitcastOp>(
563+ op.getLoc (), i8PtrTy, adaptor.getOperands ().front ());
564+ rewriter.replaceOpWithNewOp <mlir::LLVM::VaEndOp>(op, vaList);
565+ return mlir::success ();
566+ }
567+ };
568+
569+ class CIRVACopyLowering
570+ : public mlir::OpConversionPattern<mlir::cir::VACopyOp> {
571+ public:
572+ using OpConversionPattern<mlir::cir::VACopyOp>::OpConversionPattern;
573+
574+ mlir::LogicalResult
575+ matchAndRewrite (mlir::cir::VACopyOp op, OpAdaptor adaptor,
576+ mlir::ConversionPatternRewriter &rewriter) const override {
577+ auto i8PtrTy = mlir::LLVM::LLVMPointerType::get (rewriter.getI8Type ());
578+ auto dstList = rewriter.create <mlir::LLVM::BitcastOp>(
579+ op.getLoc (), i8PtrTy, adaptor.getOperands ().front ());
580+ auto srcList = rewriter.create <mlir::LLVM::BitcastOp>(
581+ op.getLoc (), i8PtrTy, adaptor.getOperands ().back ());
582+ rewriter.replaceOpWithNewOp <mlir::LLVM::VaCopyOp>(op, dstList, srcList);
583+ return mlir::success ();
584+ }
585+ };
586+
587+ class CIRVAArgLowering : public mlir ::OpConversionPattern<mlir::cir::VAArgOp> {
588+ public:
589+ using OpConversionPattern<mlir::cir::VAArgOp>::OpConversionPattern;
590+
591+ mlir::LogicalResult
592+ matchAndRewrite (mlir::cir::VAArgOp op, OpAdaptor adaptor,
593+ mlir::ConversionPatternRewriter &rewriter) const override {
594+ return op.emitError (" cir.vaarg lowering is NYI" );
595+ }
596+ };
597+
537598class CIRFuncLowering : public mlir ::OpConversionPattern<mlir::cir::FuncOp> {
538599public:
539600 using OpConversionPattern<mlir::cir::FuncOp>::OpConversionPattern;
@@ -987,7 +1048,8 @@ void populateCIRToLLVMConversionPatterns(mlir::RewritePatternSet &patterns,
9871048 CIRBinOpLowering, CIRLoadLowering, CIRConstantLowering,
9881049 CIRStoreLowering, CIRAllocaLowering, CIRFuncLowering,
9891050 CIRScopeOpLowering, CIRCastOpLowering, CIRIfLowering,
990- CIRGlobalOpLowering, CIRGetGlobalOpLowering>(
1051+ CIRGlobalOpLowering, CIRGetGlobalOpLowering, CIRVAStartLowering,
1052+ CIRVAEndLowering, CIRVACopyLowering, CIRVAArgLowering>(
9911053 converter, patterns.getContext ());
9921054}
9931055
@@ -1010,6 +1072,16 @@ mlir::LLVMTypeConverter prepareTypeConverter(mlir::MLIRContext *ctx) {
10101072 // LLVM doesn't work with signed types, so we drop the CIR signs here.
10111073 return mlir::IntegerType::get (type.getContext (), type.getWidth ());
10121074 });
1075+ converter.addConversion ([&](mlir::cir::StructType type) -> mlir::Type {
1076+ llvm::SmallVector<mlir::Type> llvmMembers;
1077+ for (auto ty : type.getMembers ())
1078+ llvmMembers.push_back (converter.convertType (ty));
1079+ auto llvmStruct = mlir::LLVM::LLVMStructType::getIdentified (
1080+ type.getContext (), type.getTypeName ());
1081+ if (llvmStruct.setBody (llvmMembers, /* isPacked=*/ type.getPacked ()).failed ())
1082+ llvm_unreachable (" Failed to set body of struct" );
1083+ return llvmStruct;
1084+ });
10131085
10141086 return converter;
10151087}
0 commit comments