Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support lowering VAArgOp #865

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ class LoweringPrepareItaniumCXXABI : public cir::LoweringPrepareCXXABI {
clang::ASTContext &astCtx,
mlir::cir::DynamicCastOp op) override;
mlir::Value lowerVAArg(cir::CIRBaseBuilderTy &builder, mlir::cir::VAArgOp op,
const cir::CIRDataLayout &datalayout) override;
const cir::CIRDataLayout &datalayout) override {
// Itanium C++ ABI has nothing to do with VAArg.
return {};
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,3 @@ LoweringPrepareItaniumCXXABI::lowerDynamicCast(CIRBaseBuilderTy &builder,
})
.getResult();
}

mlir::Value LoweringPrepareItaniumCXXABI::lowerVAArg(
CIRBaseBuilderTy &builder, mlir::cir::VAArgOp op,
const ::cir::CIRDataLayout &datalayout) {
// There is no generic cir lowering for var_arg, here we fail
// so to prevent attempt of calling lowerVAArg for ItaniumCXXABI
llvm_unreachable("NYI");
}
7 changes: 6 additions & 1 deletion clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,12 @@ class CIRVAArgLowering : public mlir::OpConversionPattern<mlir::cir::VAArgOp> {
mlir::LogicalResult
matchAndRewrite(mlir::cir::VAArgOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {
return op.emitError("cir.vaarg lowering is NYI");
auto vaArgResTy = getTypeConverter()->convertType(op.getType());
auto opaquePtr = mlir::LLVM::LLVMPointerType::get(getContext());
auto va_list = rewriter.create<mlir::LLVM::BitcastOp>(
op.getLoc(), opaquePtr, adaptor.getOperands().front());
rewriter.replaceOpWithNewOp<mlir::LLVM::VaArgOp>(op, vaArgResTy, va_list);
return mlir::success();
}
};

Expand Down
8 changes: 8 additions & 0 deletions clang/test/CIR/CodeGen/var-arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-cir -mmlir --mlir-print-ir-after=cir-lowering-prepare %s -o %t.cir 2>&1 | FileCheck %s -check-prefix=AFTER
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-llvm %s -o %t.ll
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM-X86
#include <stdarg.h>

int f1(int n, ...) {
Expand Down Expand Up @@ -118,3 +120,9 @@ int f1(int n, ...) {
// LLVM: store i32 [[RES]], ptr [[RETP]], align 4,
// LLVM: [[RETV:%.*]] = load i32, ptr [[RETP]], align 4,
// LLVM-NEXT: ret i32 [[RETV]],

// LLVM-X86: %struct.__va_list_tag = type { i32, i32, ptr, ptr }
// LLVM-X86: define {{.*}} @f1
// LLVM-X86: call void @llvm.va_start.p0(
// LLVM-X86: va_arg ptr %{{.*}}, i32
// LLVM-X86: call void @llvm.va_end.p0(
20 changes: 20 additions & 0 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,26 @@ def LLVM_LandingpadOp : LLVM_Op<"landingpad"> {
let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
// VAArgOp
//===----------------------------------------------------------------------===//

def LLVM_VaArgOp : LLVM_Op<"va_arg"> {
let arguments = (ins LLVM_AnyPointer:$arg);
let results = (outs LLVM_Type:$res);

let builders = [LLVM_OneResultOpBuilder];

let llvmBuilder = " $res = builder.CreateVAArg($arg, $_resultType); ";
let assemblyFormat = "$arg attr-dict `:` `(` type($arg) `)` `->` type($res)";

string llvmInstName = "VAArg";
string mlirBuilder = [{
$res = $_builder.create<mlir::LLVM::VaArgOp>(
$_location, $_resultType, $arg);
}];
}

//===----------------------------------------------------------------------===//
// CallOp
//===----------------------------------------------------------------------===//
Expand Down
10 changes: 6 additions & 4 deletions mlir/test/Target/LLVMIR/llvmir.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -2244,8 +2244,8 @@ llvm.func @qux(f32)

// CHECK: %struct.va_list = type { ptr }

// CHECK: define void @vararg_function(i32 %{{.*}}, ...)
llvm.func @vararg_function(%arg0: i32, ...) {
// CHECK: define i32 @vararg_function(i32 %{{.*}}, ...)
llvm.func @vararg_function(%arg0: i32, ...) -> i32 {
%0 = llvm.mlir.constant(1 : i32) : i32
%1 = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[ALLOCA0:.+]] = alloca %struct.va_list, align 8
Expand All @@ -2256,12 +2256,14 @@ llvm.func @vararg_function(%arg0: i32, ...) {
%4 = llvm.alloca %0 x !llvm.ptr {alignment = 8 : i64} : (i32) -> !llvm.ptr
// CHECK: call void @llvm.va_copy.p0(ptr %[[ALLOCA1]], ptr %[[ALLOCA0]])
llvm.intr.vacopy %2 to %4 : !llvm.ptr, !llvm.ptr
// CHECK: %[[RET:.+]] = va_arg ptr %[[ALLOCA1]], i32
%ret = llvm.va_arg %4 : (!llvm.ptr) -> i32
// CHECK: call void @llvm.va_end.p0(ptr %[[ALLOCA1]])
// CHECK: call void @llvm.va_end.p0(ptr %[[ALLOCA0]])
llvm.intr.vaend %4 : !llvm.ptr
llvm.intr.vaend %2 : !llvm.ptr
// CHECK: ret void
llvm.return
// CHECK: ret i32 %[[RET]]
llvm.return %ret : i32
}

// -----
Expand Down
Loading