From 3c2358bd6e2925212220a2abae966ffe9f835723 Mon Sep 17 00:00:00 2001 From: Sirui Mu Date: Wed, 19 Mar 2025 09:56:37 +0800 Subject: [PATCH 01/18] [CIR] Emit nsw flag for unary integer operations (#1485) Resolves #1477 . --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 8 ++++++-- clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 17 ++++++++--------- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 15 +++++++++------ clang/test/CIR/CodeGen/bitfields.c | 2 +- clang/test/CIR/CodeGen/int128.cpp | 4 ++-- clang/test/CIR/CodeGen/static-vars.c | 2 +- clang/test/CIR/CodeGen/static-vars.cpp | 2 +- clang/test/CIR/CodeGen/throw.cpp | 16 ++++++++-------- clang/test/CIR/CodeGen/try-catch.cpp | 2 +- clang/test/CIR/CodeGen/unary.cpp | 12 ++++++------ clang/test/CIR/Lowering/switch-while.c | 4 ++-- clang/test/CIR/Transforms/mem2reg.c | 4 ++-- 12 files changed, 47 insertions(+), 41 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 75fcdb4d360b..f30086af49ac 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -1146,10 +1146,14 @@ def UnaryOp : CIR_Op<"unary", [Pure, SameOperandsAndResultType]> { }]; let results = (outs CIR_AnyType:$result); - let arguments = (ins Arg:$kind, Arg:$input); + let arguments = (ins Arg:$kind, + Arg:$input, + UnitAttr:$no_signed_wrap); let assemblyFormat = [{ - `(` $kind `,` $input `)` `:` type($input) `,` type($result) attr-dict + `(` $kind `,` $input `)` + (`nsw` $no_signed_wrap^)? + `:` type($input) `,` type($result) attr-dict }]; let hasVerifier = 1; diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index de92f0f19778..d9dc455cf374 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -467,7 +467,7 @@ class ScalarExprEmitter : public StmtVisitor { auto Kind = E->isIncrementOp() ? cir::UnaryOpKind::Inc : cir::UnaryOpKind::Dec; // NOTE(CIR): clang calls CreateAdd but folds this to a unary op - value = emitUnaryOp(E, Kind, input); + value = emitUnaryOp(E, Kind, input, /*nsw=*/false); } // Next most common: pointer increment. } else if (const PointerType *ptr = type->getAs()) { @@ -580,22 +580,20 @@ class ScalarExprEmitter : public StmtVisitor { mlir::Value emitIncDecConsiderOverflowBehavior(const UnaryOperator *E, mlir::Value InVal, bool IsInc) { - // NOTE(CIR): The SignedOverflowBehavior is attached to the global ModuleOp - // and the nsw behavior is handled during lowering. auto Kind = E->isIncrementOp() ? cir::UnaryOpKind::Inc : cir::UnaryOpKind::Dec; switch (CGF.getLangOpts().getSignedOverflowBehavior()) { case LangOptions::SOB_Defined: - return emitUnaryOp(E, Kind, InVal); + return emitUnaryOp(E, Kind, InVal, /*nsw=*/false); case LangOptions::SOB_Undefined: if (!CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) - return emitUnaryOp(E, Kind, InVal); + return emitUnaryOp(E, Kind, InVal, /*nsw=*/true); llvm_unreachable( "inc/dec overflow behavior SOB_Undefined not implemented yet"); break; case LangOptions::SOB_Trapping: if (!E->canOverflow()) - return emitUnaryOp(E, Kind, InVal); + return emitUnaryOp(E, Kind, InVal, /*nsw=*/true); llvm_unreachable( "inc/dec overflow behavior SOB_Trapping not implemented yet"); break; @@ -661,7 +659,8 @@ class ScalarExprEmitter : public StmtVisitor { // NOTE: LLVM codegen will lower this directly to either a FNeg // or a Sub instruction. In CIR this will be handled later in LowerToLLVM. - return emitUnaryOp(E, cir::UnaryOpKind::Minus, operand); + return emitUnaryOp(E, cir::UnaryOpKind::Minus, operand, + /*nsw=*/E->getType()->isSignedIntegerType()); } mlir::Value VisitUnaryNot(const UnaryOperator *E) { @@ -684,10 +683,10 @@ class ScalarExprEmitter : public StmtVisitor { } mlir::Value emitUnaryOp(const UnaryOperator *E, cir::UnaryOpKind kind, - mlir::Value input) { + mlir::Value input, bool nsw = false) { return Builder.create( CGF.getLoc(E->getSourceRange().getBegin()), input.getType(), kind, - input); + input, nsw); } // C++ diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 4d56e16db784..c014bab10d3e 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -2553,21 +2553,24 @@ mlir::LogicalResult CIRToLLVMUnaryOpLowering::matchAndRewrite( // Integer unary operations: + - ~ ++ -- if (mlir::isa(elementType)) { + auto overflowFlags = op.getNoSignedWrap() + ? mlir::LLVM::IntegerOverflowFlags::nsw + : mlir::LLVM::IntegerOverflowFlags::none; switch (op.getKind()) { case cir::UnaryOpKind::Inc: { assert(!IsVector && "++ not allowed on vector types"); auto One = rewriter.create( loc, llvmType, mlir::IntegerAttr::get(llvmType, 1)); - rewriter.replaceOpWithNewOp(op, llvmType, - adaptor.getInput(), One); + rewriter.replaceOpWithNewOp( + op, llvmType, adaptor.getInput(), One, overflowFlags); return mlir::success(); } case cir::UnaryOpKind::Dec: { assert(!IsVector && "-- not allowed on vector types"); auto One = rewriter.create( loc, llvmType, mlir::IntegerAttr::get(llvmType, 1)); - rewriter.replaceOpWithNewOp(op, llvmType, - adaptor.getInput(), One); + rewriter.replaceOpWithNewOp( + op, llvmType, adaptor.getInput(), One, overflowFlags); return mlir::success(); } case cir::UnaryOpKind::Plus: { @@ -2581,8 +2584,8 @@ mlir::LogicalResult CIRToLLVMUnaryOpLowering::matchAndRewrite( else Zero = rewriter.create( loc, llvmType, mlir::IntegerAttr::get(llvmType, 0)); - rewriter.replaceOpWithNewOp(op, llvmType, Zero, - adaptor.getInput()); + rewriter.replaceOpWithNewOp( + op, llvmType, Zero, adaptor.getInput(), overflowFlags); return mlir::success(); } case cir::UnaryOpKind::Not: { diff --git a/clang/test/CIR/CodeGen/bitfields.c b/clang/test/CIR/CodeGen/bitfields.c index cd8e88f5c4c9..39511a3401cf 100644 --- a/clang/test/CIR/CodeGen/bitfields.c +++ b/clang/test/CIR/CodeGen/bitfields.c @@ -89,7 +89,7 @@ int load_field(S* s) { // CHECK: cir.func {{.*@unOp}} // CHECK: [[TMP0:%.*]] = cir.get_member {{.*}}[1] {name = "d"} : !cir.ptr -> !cir.ptr> // CHECK: [[TMP1:%.*]] = cir.get_bitfield(#bfi_d, [[TMP0]] : !cir.ptr>) -> !s32i -// CHECK: [[TMP2:%.*]] = cir.unary(inc, [[TMP1]]) : !s32i, !s32i +// CHECK: [[TMP2:%.*]] = cir.unary(inc, [[TMP1]]) nsw : !s32i, !s32i // CHECK: cir.set_bitfield(#bfi_d, [[TMP0]] : !cir.ptr>, [[TMP2]] : !s32i) void unOp(S* s) { s->d++; diff --git a/clang/test/CIR/CodeGen/int128.cpp b/clang/test/CIR/CodeGen/int128.cpp index 97539e4317c9..5244b9d2fa62 100644 --- a/clang/test/CIR/CodeGen/int128.cpp +++ b/clang/test/CIR/CodeGen/int128.cpp @@ -26,8 +26,8 @@ unsigned __int128 test2(unsigned __int128 x) { // LLVM-LABEL: @_Z11unary_arithn __int128 unary_arith(__int128 x) { return ++x; - // CHECK: %{{.+}} = cir.unary(inc, %{{.+}}) : !s128i, !s128i - // LLVM: %{{.+}} = add i128 %{{.+}}, 1 + // CHECK: %{{.+}} = cir.unary(inc, %{{.+}}) nsw : !s128i, !s128i + // LLVM: %{{.+}} = add nsw i128 %{{.+}}, 1 } // CHECK-LABEL: @_Z12binary_arithnn diff --git a/clang/test/CIR/CodeGen/static-vars.c b/clang/test/CIR/CodeGen/static-vars.c index 140f4e6052f6..5cc2158059cc 100644 --- a/clang/test/CIR/CodeGen/static-vars.c +++ b/clang/test/CIR/CodeGen/static-vars.c @@ -24,7 +24,7 @@ void func1(void) { j++; // CHECK-DAG: %[[#V2:]] = cir.get_global @func1.j : !cir.ptr // CHECK-DAG: %[[#V3:]] = cir.load %[[#V2]] : !cir.ptr, !s32i - // CHECK-DAG: %[[#V4:]] = cir.unary(inc, %[[#V3]]) : !s32i, !s32i + // CHECK-DAG: %[[#V4:]] = cir.unary(inc, %[[#V3]]) nsw : !s32i, !s32i // CHECK-DAG: cir.store %[[#V4]], %[[#V2]] : !s32i, !cir.ptr } diff --git a/clang/test/CIR/CodeGen/static-vars.cpp b/clang/test/CIR/CodeGen/static-vars.cpp index c8d1ff5ed439..300629cacf01 100644 --- a/clang/test/CIR/CodeGen/static-vars.cpp +++ b/clang/test/CIR/CodeGen/static-vars.cpp @@ -26,7 +26,7 @@ void func1(void) { j++; // CHECK-DAG: %[[#V2:]] = cir.get_global @_ZZ5func1vE1j : !cir.ptr // CHECK-DAG: %[[#V3:]] = cir.load %[[#V2]] : !cir.ptr, !s32i - // CHECK-DAG: %[[#V4:]] = cir.unary(inc, %[[#V3]]) : !s32i, !s32i + // CHECK-DAG: %[[#V4:]] = cir.unary(inc, %[[#V3]]) nsw : !s32i, !s32i // CHECK-DAG: cir.store %[[#V4]], %[[#V2]] : !s32i, !cir.ptr } diff --git a/clang/test/CIR/CodeGen/throw.cpp b/clang/test/CIR/CodeGen/throw.cpp index dcaf2ce11f58..88dfe4284295 100644 --- a/clang/test/CIR/CodeGen/throw.cpp +++ b/clang/test/CIR/CodeGen/throw.cpp @@ -52,7 +52,7 @@ void refoo1() { // CIR: } catch [type #cir.all { // CIR: %[[V3:.*]] = cir.catch_param -> !cir.ptr // CIR: %[[V4:.*]] = cir.load %[[V0]] : !cir.ptr, !s32i -// CIR: %[[V5:.*]] = cir.unary(inc, %[[V4]]) : !s32i, !s32i +// CIR: %[[V5:.*]] = cir.unary(inc, %[[V4]]) nsw : !s32i, !s32i // CIR: cir.store %[[V5]], %[[V0]] : !s32i, !cir.ptr // CIR: cir.yield // CIR: }] @@ -91,7 +91,7 @@ void refoo1() { // LLVM: %[[V16:.*]] = phi ptr [ %[[V9]], %[[B7]] ], [ %[[V13]], %[[B11]] ] // LLVM: %[[V17:.*]] = call ptr @__cxa_begin_catch(ptr %[[V16]]) // LLVM: %[[V18:.*]] = load i32, ptr %[[V2]], align 4 -// LLVM: %[[V19:.*]] = add i32 %[[V18]], 1 +// LLVM: %[[V19:.*]] = add nsw i32 %[[V18]], 1 // LLVM: store i32 %[[V19]], ptr %[[V2]], align 4 // LLVM: call void @__cxa_end_catch() @@ -136,7 +136,7 @@ void refoo2() { // CIR: cir.yield // CIR: } step { // CIR: %[[V5:.*]] = cir.load %[[V3]] : !cir.ptr, !s32i -// CIR: %[[V6:.*]] = cir.unary(inc, %[[V5]]) : !s32i, !s32i +// CIR: %[[V6:.*]] = cir.unary(inc, %[[V5]]) nsw : !s32i, !s32i // CIR: cir.store %[[V6]], %[[V3]] : !s32i, !cir.ptr // CIR: cir.yield // CIR: } @@ -146,7 +146,7 @@ void refoo2() { // CIR: } catch [type #cir.all { // CIR: %[[V3:.*]] = cir.catch_param -> !cir.ptr // CIR: %[[V4:.*]] = cir.load %[[V0]] : !cir.ptr, !s32i -// CIR: %[[V5:.*]] = cir.unary(inc, %[[V4]]) : !s32i, !s32i +// CIR: %[[V5:.*]] = cir.unary(inc, %[[V4]]) nsw : !s32i, !s32i // CIR: cir.store %[[V5]], %[[V0]] : !s32i, !cir.ptr // CIR: cir.yield // CIR: }] @@ -166,7 +166,7 @@ void refoo2() { // LLVM: br label %[[B16:.*]] // LLVM: [[B16]]: // LLVM: %[[V17]] = load i32, ptr {{.*}}, align 4 -// LLVM: %[[V18]] = add i32 %[[V17]], 1 +// LLVM: %[[V18]] = add nsw i32 %[[V17]], 1 // LLVM: store i32 %[[V18]], ptr {{.*}}, align 4 // LLVM: br label {{.*}} // LLVM: %[[B19:.*]] @@ -198,7 +198,7 @@ void refoo2() { // LLVM: %[[V35:.*]] = phi ptr [ %[[V32]], %[[B30]] ], [ %[[V24]], %[[B22]] ], [ %[[V28]], %[[B26]] ] // LLVM: %[[V36:.*]] = call ptr @__cxa_begin_catch(ptr %[[V35]]) // LLVM: %[[V37:.*]] = load i32, ptr {{.*}}, align 4 -// LLVM: %[[V38:.*]] = add i32 %[[V37]], 1 +// LLVM: %[[V38:.*]] = add nsw i32 %[[V37]], 1 // LLVM: store i32 %[[V38]], ptr {{.*}}, align 4 // LLVM: call void @__cxa_end_catch() // LLVM: br label {{.*}} @@ -228,7 +228,7 @@ void refoo3() { // CIR: } catch [type #cir.all { // CIR: %[[V3:.*]] = cir.catch_param -> !cir.ptr // CIR: %[[V4:.*]] = cir.load %[[V0]] : !cir.ptr, !s32i -// CIR: %[[V5:.*]] = cir.unary(inc, %[[V4]]) : !s32i, !s32i +// CIR: %[[V5:.*]] = cir.unary(inc, %[[V4]]) nsw : !s32i, !s32i // CIR: cir.store %[[V5]], %[[V0]] : !s32i, !cir.ptr // CIR: cir.yield // CIR: }] @@ -261,7 +261,7 @@ void refoo3() { // LLVM: %[[V17:.*]] = phi ptr [ %[[V14]], %[[B12]] ], [ %[[V10]], %[[B8]] ] // LLVM: %[[V18:.*]] = call ptr @__cxa_begin_catch(ptr %[[V17]]) // LLVM: %[[V19:.*]] = load i32, ptr {{.*}}, align 4 -// LLVM: %[[V20:.*]] = add i32 %[[V19]], 1 +// LLVM: %[[V20:.*]] = add nsw i32 %[[V19]], 1 // LLVM: store i32 %[[V20]], ptr {{.*}}, align 4 // LLVM: call void @__cxa_end_catch() // LLVM: br label %[[B21]] diff --git a/clang/test/CIR/CodeGen/try-catch.cpp b/clang/test/CIR/CodeGen/try-catch.cpp index 570017f83553..80297dd5c1b5 100644 --- a/clang/test/CIR/CodeGen/try-catch.cpp +++ b/clang/test/CIR/CodeGen/try-catch.cpp @@ -144,7 +144,7 @@ void tc6() { // CHECK: cir.return // CHECK: ^bb1: // no predecessors // CHECK: %[[V2:.*]] = cir.load {{.*}} : !cir.ptr, !s32i -// CHECK: %[[V3:.*]] = cir.unary(inc, %[[V2]]) : !s32i, !s32i +// CHECK: %[[V3:.*]] = cir.unary(inc, %[[V2]]) nsw : !s32i, !s32i // CHECK: cir.store %[[V3]], {{.*}} : !s32i, !cir.ptr // CHECK: cir.yield // CHECK: } diff --git a/clang/test/CIR/CodeGen/unary.cpp b/clang/test/CIR/CodeGen/unary.cpp index 986e9b2dcedc..d86f9908e5b7 100644 --- a/clang/test/CIR/CodeGen/unary.cpp +++ b/clang/test/CIR/CodeGen/unary.cpp @@ -49,7 +49,7 @@ int inc0() { // CHECK: %[[#ATMP:]] = cir.const #cir.int<1> : !s32i // CHECK: cir.store %[[#ATMP]], %[[#A]] : !s32i // CHECK: %[[#INPUT:]] = cir.load %[[#A]] -// CHECK: %[[#INCREMENTED:]] = cir.unary(inc, %[[#INPUT]]) +// CHECK: %[[#INCREMENTED:]] = cir.unary(inc, %[[#INPUT]]) nsw // CHECK: cir.store %[[#INCREMENTED]], %[[#A]] // CHECK: %[[#A_TO_OUTPUT:]] = cir.load %[[#A]] // CHECK: cir.store %[[#A_TO_OUTPUT]], %[[#RET]] @@ -68,7 +68,7 @@ int dec0() { // CHECK: %[[#ATMP:]] = cir.const #cir.int<1> : !s32i // CHECK: cir.store %[[#ATMP]], %[[#A]] : !s32i // CHECK: %[[#INPUT:]] = cir.load %[[#A]] -// CHECK: %[[#INCREMENTED:]] = cir.unary(dec, %[[#INPUT]]) +// CHECK: %[[#INCREMENTED:]] = cir.unary(dec, %[[#INPUT]]) nsw // CHECK: cir.store %[[#INCREMENTED]], %[[#A]] // CHECK: %[[#A_TO_OUTPUT:]] = cir.load %[[#A]] // CHECK: cir.store %[[#A_TO_OUTPUT]], %[[#RET]] @@ -88,7 +88,7 @@ int inc1() { // CHECK: %[[#ATMP:]] = cir.const #cir.int<1> : !s32i // CHECK: cir.store %[[#ATMP]], %[[#A]] : !s32i // CHECK: %[[#INPUT:]] = cir.load %[[#A]] -// CHECK: %[[#INCREMENTED:]] = cir.unary(inc, %[[#INPUT]]) +// CHECK: %[[#INCREMENTED:]] = cir.unary(inc, %[[#INPUT]]) nsw // CHECK: cir.store %[[#INCREMENTED]], %[[#A]] // CHECK: %[[#A_TO_OUTPUT:]] = cir.load %[[#A]] // CHECK: cir.store %[[#A_TO_OUTPUT]], %[[#RET]] @@ -107,7 +107,7 @@ int dec1() { // CHECK: %[[#ATMP:]] = cir.const #cir.int<1> : !s32i // CHECK: cir.store %[[#ATMP]], %[[#A]] : !s32i // CHECK: %[[#INPUT:]] = cir.load %[[#A]] -// CHECK: %[[#INCREMENTED:]] = cir.unary(dec, %[[#INPUT]]) +// CHECK: %[[#INCREMENTED:]] = cir.unary(dec, %[[#INPUT]]) nsw // CHECK: cir.store %[[#INCREMENTED]], %[[#A]] // CHECK: %[[#A_TO_OUTPUT:]] = cir.load %[[#A]] // CHECK: cir.store %[[#A_TO_OUTPUT]], %[[#RET]] @@ -128,7 +128,7 @@ int inc2() { // CHECK: %[[#ATMP:]] = cir.const #cir.int<1> : !s32i // CHECK: cir.store %[[#ATMP]], %[[#A]] : !s32i // CHECK: %[[#ATOB:]] = cir.load %[[#A]] -// CHECK: %[[#INCREMENTED:]] = cir.unary(inc, %[[#ATOB]]) +// CHECK: %[[#INCREMENTED:]] = cir.unary(inc, %[[#ATOB]]) nsw // CHECK: cir.store %[[#INCREMENTED]], %[[#A]] // CHECK: cir.store %[[#ATOB]], %[[#B]] // CHECK: %[[#B_TO_OUTPUT:]] = cir.load %[[#B]] @@ -218,7 +218,7 @@ void chars(char c) { // CHECK: cir.unary(plus, %[[#PROMO]]) : !s32i, !s32i int c2 = -c; // CHECK: %[[#PROMO:]] = cir.cast(integral, %{{.+}} : !s8i), !s32i - // CHECK: cir.unary(minus, %[[#PROMO]]) : !s32i, !s32i + // CHECK: cir.unary(minus, %[[#PROMO]]) nsw : !s32i, !s32i // Chars can go through some integer promotion codegen paths even when not promoted. ++c; // CHECK: cir.unary(inc, %10) : !s8i, !s8i diff --git a/clang/test/CIR/Lowering/switch-while.c b/clang/test/CIR/Lowering/switch-while.c index 078f0045716c..9123d5532960 100644 --- a/clang/test/CIR/Lowering/switch-while.c +++ b/clang/test/CIR/Lowering/switch-while.c @@ -42,7 +42,7 @@ int f(int a, int cond) { // CHECK: br label %[[LOOP_HEADER:.+]] // // CHECK: [[LOOP_HEADER]]: -// CHECK: add i32 %{{.*}}, 1 +// CHECK: add nsw i32 %{{.*}}, 1 // CHECK: br label %[[DEFAULT_BB:.+]] // // CHECK: [[DEFAULT_BB]]: @@ -61,7 +61,7 @@ int f(int a, int cond) { // CHECK: add nsw i32 %[[V1]], %[[V2]] // // CHECK: [[TWO_BB]]: -// CHECK: add i32 %{{.*}}, 1 +// CHECK: add nsw i32 %{{.*}}, 1 // CHECK: br label %[[FALLTHOUGH_BB:.+]] // // CHECK: [[FALLTHOUGH_BB]]: diff --git a/clang/test/CIR/Transforms/mem2reg.c b/clang/test/CIR/Transforms/mem2reg.c index b60d9eb0d1e9..6e0ad9a4e529 100644 --- a/clang/test/CIR/Transforms/mem2reg.c +++ b/clang/test/CIR/Transforms/mem2reg.c @@ -57,7 +57,7 @@ void alloca_in_loop(int* ar, int n) { // BEFORE: cir.yield // BEFORE: } step { // BEFORE: %4 = cir.load %2 : !cir.ptr, !s32i -// BEFORE: %5 = cir.unary(inc, %4) : !s32i, !s32i +// BEFORE: %5 = cir.unary(inc, %4) nsw : !s32i, !s32i // BEFORE: cir.store %5, %2 : !s32i, !cir.ptr // BEFORE: cir.yield // BEFORE: } @@ -82,7 +82,7 @@ void alloca_in_loop(int* ar, int n) { // MEM2REG: ^bb5: // pred: ^bb4 // MEM2REG: cir.br ^bb6 // MEM2REG: ^bb6: // pred: ^bb5 -// MEM2REG: %5 = cir.unary(inc, %1) : !s32i, !s32i +// MEM2REG: %5 = cir.unary(inc, %1) nsw : !s32i, !s32i // MEM2REG: cir.br ^bb2(%5 : !s32i) // MEM2REG: ^bb7: // pred: ^bb2 // MEM2REG: cir.br ^bb8 From 11433965a105c146b4457719be601701e9db1e55 Mon Sep 17 00:00:00 2001 From: Amr Hesham Date: Wed, 19 Mar 2025 02:59:30 +0100 Subject: [PATCH 02/18] [CIR][CIRGen][Builtin][Neon] Lower vcales_f32 (#1500) Lower vcales_f32 --- clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp | 3 +-- clang/test/CIR/CodeGen/AArch64/neon.c | 16 ++++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp index 2db37550eee3..ef09403264ac 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp @@ -2708,11 +2708,10 @@ static mlir::Value emitCommonNeonSISDBuiltinExpr( case NEON::BI__builtin_neon_vcagtd_f64: return emitNeonCall(builder, {argTy}, ops, "aarch64.neon.facgt", resultTy, loc); + case NEON::BI__builtin_neon_vcales_f32: case NEON::BI__builtin_neon_vcaled_f64: return emitNeonCall(builder, {argTy}, ops, "aarch64.neon.facge", resultTy, loc); - case NEON::BI__builtin_neon_vcales_f32: - llvm_unreachable(" neon_vcales_f32 NYI "); case NEON::BI__builtin_neon_vcaltd_f64: llvm_unreachable(" neon_vcaltd_f64 NYI "); case NEON::BI__builtin_neon_vcalts_f32: diff --git a/clang/test/CIR/CodeGen/AArch64/neon.c b/clang/test/CIR/CodeGen/AArch64/neon.c index 942d32aebe1a..6419c43e4baa 100644 --- a/clang/test/CIR/CodeGen/AArch64/neon.c +++ b/clang/test/CIR/CodeGen/AArch64/neon.c @@ -15156,12 +15156,16 @@ uint64_t test_vcagtd_f64(float64_t a, float64_t b) { // LLVM: ret i64 [[VCAGED_F64_I]] } -// NYI-LABEL: @test_vcales_f32( -// NYI: [[VCALES_F32_I:%.*]] = call i32 @llvm.aarch64.neon.facge.i32.f32(float %b, float %a) -// NYI: ret i32 [[VCALES_F32_I]] -// uint32_t test_vcales_f32(float32_t a, float32_t b) { -// return (uint32_t)vcales_f32(a, b); -// } +uint32_t test_vcales_f32(float32_t a, float32_t b) { + return (uint32_t)vcales_f32(a, b); + + // CIR-LABEL: vcales_f32 + // CIR: [[TMP0:%.*]] = cir.llvm.intrinsic "aarch64.neon.facge" {{.*}}, {{.*}} : (!cir.float, !cir.float) -> !u32i + + // LLVM-LABEL: @test_vcales_f32( + // LLVM: [[VCALES_F32_I:%.*]] = call i32 @llvm.aarch64.neon.facge.i32.f32(float %0, float %1) + // LLVM: ret i32 [[VCALES_F32_I]] +} uint64_t test_vcaled_f64(float64_t a, float64_t b) { return (uint64_t)vcaled_f64(a, b); From c53085c969daf9f1c506a43c56c58c71f4fb1616 Mon Sep 17 00:00:00 2001 From: "Chibuoyim (Wilson) Ogbonna" Date: Wed, 19 Mar 2025 05:01:48 +0300 Subject: [PATCH 03/18] [CIR][CodeGen] Add InsertionGuard for tryBodyScope (#1498) This PR adds an insertion guard for the try body scope for try-catch. Currently, the following code snippet fails during CodeGen: ``` void foo() { int r = 1; try { ++r; return; } catch (...) { } } ``` The insertion point doesn't get reset properly and the cleanup is being ran for a wrong/deleted block causing a segmentation fault. I also added a test. --- clang/lib/CIR/CodeGen/CIRGenException.cpp | 1 + clang/test/CIR/CodeGen/try-catch.cpp | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/clang/lib/CIR/CodeGen/CIRGenException.cpp b/clang/lib/CIR/CodeGen/CIRGenException.cpp index 4ee25d0d38eb..022607dc3dd6 100644 --- a/clang/lib/CIR/CodeGen/CIRGenException.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenException.cpp @@ -382,6 +382,7 @@ CIRGenFunction::emitCXXTryStmtUnderScope(const CXXTryStmt &S) { enterCXXTryStmt(S, tryOp); // Emit the body for the `try {}` part. { + mlir::OpBuilder::InsertionGuard guard(getBuilder()); CIRGenFunction::LexicalScope tryBodyScope{ *this, loc, getBuilder().getInsertionBlock()}; if (emitStmt(S.getTryBlock(), /*useCurrentScope=*/true).failed()) diff --git a/clang/test/CIR/CodeGen/try-catch.cpp b/clang/test/CIR/CodeGen/try-catch.cpp index 80297dd5c1b5..400a993991e3 100644 --- a/clang/test/CIR/CodeGen/try-catch.cpp +++ b/clang/test/CIR/CodeGen/try-catch.cpp @@ -149,3 +149,22 @@ void tc6() { // CHECK: cir.yield // CHECK: } // CHECK: } + +// CHECK: cir.func @_Z3tc7v() +void tc7() { + int r = 1; + try { + ++r; + return; + } catch (...) { + } +} + +// CHECK: cir.scope { +// CHECK: cir.try { +// CHECK: %[[V2:.*]] = cir.load {{.*}} : !cir.ptr, !s32i +// CHECK: %[[V3:.*]] = cir.unary(inc, %[[V2]]) : !s32i, !s32i +// CHECK: cir.store %[[V3]], {{.*}} : !s32i, !cir.ptr +// CHECK: cir.return +// CHECK: } +// CHECK: } From 6ce6b9ab797f42cf928fad8c0c2c3d93c1a1b6fa Mon Sep 17 00:00:00 2001 From: Yue Huang <30948580+AdUhTkJm@users.noreply.github.com> Date: Wed, 19 Mar 2025 02:19:17 +0000 Subject: [PATCH 04/18] [CIR][NFC] Generalize IdiomRecognizer (#1484) The comments suggested that we should use TableGen to generate the recognizing functions. However, I think templates might be more suitable for generating them -- and I can't find any existing TableGen backends that let us generate arbitrary functions. My choice of design is to offer a template to match standard library functions: ```cpp // matches std::find with 3 arguments, and raise it into StdFindOp StdRecognizer<3, StdFindOp, StdFuncsID::Find> ``` I have to use a TableGen'd enum to map names to IDs, as we can't pass string literals to template arguments easily in C++17. This also constraints design of future `StdXXXOp`s: they must take operands the same way of StdFindOp, where the first one is the original function, and the rest are function arguments. I'm not sure if this approach is the best way. Please tell me if you have concerns or any alternative ways. --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 72 ++--------------- .../include/clang/CIR/Dialect/IR/CIRStdOps.td | 66 ++++++++++++++++ .../Dialect/Transforms/IdiomRecognizer.cpp | 79 ++++++++++++------- .../Dialect/Transforms/LoweringPrepare.cpp | 6 +- .../test/CIR/Transforms/idiom-recognizer.cpp | 11 ++- 5 files changed, 131 insertions(+), 103 deletions(-) create mode 100644 clang/include/clang/CIR/Dialect/IR/CIRStdOps.td diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index f30086af49ac..3f00dda0f904 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -4700,72 +4700,6 @@ def FrameAddrOp : FuncAddrBuiltinOp<"frame_address"> { }]; } -//===----------------------------------------------------------------------===// -// StdFindOp -//===----------------------------------------------------------------------===// - -def StdFindOp : CIR_Op<"std.find", [SameFirstSecondOperandAndResultType]> { - let arguments = (ins FlatSymbolRefAttr:$original_fn, - CIR_AnyType:$first, - CIR_AnyType:$last, - CIR_AnyType:$pattern); - let summary = "std:find()"; - let results = (outs CIR_AnyType:$result); - - let description = [{ - Search for `pattern` in data range from `first` to `last`. This currently - maps to only one form of `std::find`. The `original_fn` operand tracks the - mangled named that can be used when lowering to a `cir.call`. - - Example: - - ```mlir - ... - %result = cir.std.find(@original_fn, - %first : !T, %last : !T, %pattern : !P) -> !T - ``` - }]; - - let assemblyFormat = [{ - `(` - $original_fn - `,` $first `:` type($first) - `,` $last `:` type($last) - `,` $pattern `:` type($pattern) - `)` `->` type($result) attr-dict - }]; - let hasVerifier = 0; -} - -//===----------------------------------------------------------------------===// -// IterBegin/End -//===----------------------------------------------------------------------===// - -def IterBeginOp : CIR_Op<"iterator_begin"> { - let arguments = (ins FlatSymbolRefAttr:$original_fn, CIR_AnyType:$container); - let summary = "Returns an iterator to the first element of a container"; - let results = (outs CIR_AnyType:$result); - let assemblyFormat = [{ - `(` - $original_fn `,` $container `:` type($container) - `)` `->` type($result) attr-dict - }]; - let hasVerifier = 0; -} - -def IterEndOp : CIR_Op<"iterator_end"> { - let arguments = (ins FlatSymbolRefAttr:$original_fn, CIR_AnyType:$container); - let summary = "Returns an iterator to the element following the last element" - " of a container"; - let results = (outs CIR_AnyType:$result); - let assemblyFormat = [{ - `(` - $original_fn `,` $container `:` type($container) - `)` `->` type($result) attr-dict - }]; - let hasVerifier = 0; -} - //===----------------------------------------------------------------------===// // Floating Point Ops //===----------------------------------------------------------------------===// @@ -5755,4 +5689,10 @@ def SignBitOp : CIR_Op<"signbit", [Pure]> { }]; } +//===----------------------------------------------------------------------===// +// Standard library function calls +//===----------------------------------------------------------------------===// + +include "clang/CIR/Dialect/IR/CIRStdOps.td" + #endif // LLVM_CLANG_CIR_DIALECT_IR_CIROPS diff --git a/clang/include/clang/CIR/Dialect/IR/CIRStdOps.td b/clang/include/clang/CIR/Dialect/IR/CIRStdOps.td new file mode 100644 index 000000000000..9f33498a0a3a --- /dev/null +++ b/clang/include/clang/CIR/Dialect/IR/CIRStdOps.td @@ -0,0 +1,66 @@ +//===-- CIRStdOps.td - CIR standard library ops ------------*- tablegen -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// +/// Defines ops representing standard library calls +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_CIR_DIALECT_IR_CIRSTDOPS +#define LLVM_CLANG_CIR_DIALECT_IR_CIRSTDOPS + +class CIRStdOp traits = []>: + CIR_Op<"std." # functionName, traits> { + string funcName = functionName; + + let arguments = !con((ins FlatSymbolRefAttr:$original_fn), args); + + let summary = "std::" # functionName # "()"; + let results = res; + + let extraClassDeclaration = [{ + static constexpr unsigned getNumArgs() { + return }] # !size(args) # [{; + } + static llvm::StringRef getFunctionName() { + return "}] # functionName # [{"; + } + }]; + + string argsAssemblyFormat = !interleave( + !foreach( + name, + !foreach(i, !range(!size(args)), !getdagname(args, i)), + !strconcat("$", name, " `:` type($", name, ")") + ), " `,` " + ); + + string resultAssemblyFormat = !if( + !empty(res), + "", + " `->` type($" # !getdagname(res, 0) # ")" + ); + + let assemblyFormat = !strconcat("`(` ", argsAssemblyFormat, + " `,` $original_fn `)`", resultAssemblyFormat, + " attr-dict"); + + let hasVerifier = 0; +} + +def StdFindOp : CIRStdOp<"find", + (ins CIR_AnyType:$first, CIR_AnyType:$last, CIR_AnyType:$pattern), + (outs CIR_AnyType:$result), + [SameFirstSecondOperandAndResultType]>; +def IterBeginOp: CIRStdOp<"begin", + (ins CIR_AnyType:$container), + (outs CIR_AnyType:$result)>; +def IterEndOp: CIRStdOp<"end", + (ins CIR_AnyType:$container), + (outs CIR_AnyType:$result)>; + +#endif diff --git a/clang/lib/CIR/Dialect/Transforms/IdiomRecognizer.cpp b/clang/lib/CIR/Dialect/Transforms/IdiomRecognizer.cpp index 39a1ac4ef5ce..3a1f6495b089 100644 --- a/clang/lib/CIR/Dialect/Transforms/IdiomRecognizer.cpp +++ b/clang/lib/CIR/Dialect/Transforms/IdiomRecognizer.cpp @@ -32,11 +32,52 @@ using namespace cir; namespace { +// Recognizes a cir.call that calls a standard library function represented +// by `TargetOp`, and raise it to that operation. +template class StdRecognizer { +private: + // Reserved for template specialization. + static bool checkArguments(mlir::ValueRange) { return true; } + + template + static TargetOp buildCall(CIRBaseBuilderTy &builder, CallOp call, + std::index_sequence) { + return builder.create(call.getLoc(), call.getResult().getType(), + call.getCalleeAttr(), + call.getOperand(Indices)...); + } + +public: + static bool raise(CallOp call, mlir::MLIRContext &context, bool remark) { + constexpr int numArgs = TargetOp::getNumArgs(); + if (call.getNumOperands() != numArgs) + return false; + + auto callExprAttr = call.getAstAttr(); + llvm::StringRef stdFuncName = TargetOp::getFunctionName(); + if (!callExprAttr || !callExprAttr.isStdFunctionCall(stdFuncName)) + return false; + + if (!checkArguments(call.getArgOperands())) + return false; + + if (remark) + mlir::emitRemark(call.getLoc()) + << "found call to std::" << stdFuncName << "()"; + + CIRBaseBuilderTy builder(context); + builder.setInsertionPointAfter(call.getOperation()); + TargetOp op = buildCall(builder, call, std::make_index_sequence()); + call.replaceAllUsesWith(op); + call.erase(); + return true; + } +}; + struct IdiomRecognizerPass : public IdiomRecognizerBase { IdiomRecognizerPass() = default; void runOnOperation() override; void recognizeCall(CallOp call); - bool raiseStdFind(CallOp call); bool raiseIteratorBeginEnd(CallOp call); // Handle pass options @@ -88,30 +129,6 @@ struct IdiomRecognizerPass : public IdiomRecognizerBase { }; } // namespace -bool IdiomRecognizerPass::raiseStdFind(CallOp call) { - // FIXME: tablegen all of this function. - if (call.getNumOperands() != 3) - return false; - - auto callExprAttr = call.getAstAttr(); - if (!callExprAttr || !callExprAttr.isStdFunctionCall("find")) { - return false; - } - - if (opts.emitRemarkFoundCalls()) - emitRemark(call.getLoc()) << "found call to std::find()"; - - CIRBaseBuilderTy builder(getContext()); - builder.setInsertionPointAfter(call.getOperation()); - auto findOp = builder.create( - call.getLoc(), call.getResult().getType(), call.getCalleeAttr(), - call.getOperand(0), call.getOperand(1), call.getOperand(2)); - - call.replaceAllUsesWith(findOp); - call.erase(); - return true; -} - static bool isIteratorLikeType(mlir::Type t) { // TODO: some iterators are going to be represented with structs, // in which case we could look at ASTRecordDeclInterface for more @@ -175,8 +192,16 @@ void IdiomRecognizerPass::recognizeCall(CallOp call) { if (raiseIteratorBeginEnd(call)) return; - if (raiseStdFind(call)) - return; + bool remark = opts.emitRemarkFoundCalls(); + + using StdFunctionsRecognizer = std::tuple>; + + // MSVC requires explicitly capturing these variables. + std::apply( + [&, call, remark, this](auto... recognizers) { + (decltype(recognizers)::raise(call, this->getContext(), remark) || ...); + }, + StdFunctionsRecognizer()); } void IdiomRecognizerPass::runOnOperation() { diff --git a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp index 030327867f2b..9e1a57e7f665 100644 --- a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp @@ -1481,8 +1481,7 @@ void LoweringPreparePass::lowerIterBeginOp(IterBeginOp op) { CIRBaseBuilderTy builder(getContext()); builder.setInsertionPointAfter(op.getOperation()); auto call = builder.createCallOp(op.getLoc(), op.getOriginalFnAttr(), - op.getResult().getType(), - mlir::ValueRange{op.getOperand()}); + op.getResult().getType(), op.getOperand()); op.replaceAllUsesWith(call); op.erase(); @@ -1492,8 +1491,7 @@ void LoweringPreparePass::lowerIterEndOp(IterEndOp op) { CIRBaseBuilderTy builder(getContext()); builder.setInsertionPointAfter(op.getOperation()); auto call = builder.createCallOp(op.getLoc(), op.getOriginalFnAttr(), - op.getResult().getType(), - mlir::ValueRange{op.getOperand()}); + op.getResult().getType(), op.getOperand()); op.replaceAllUsesWith(call); op.erase(); diff --git a/clang/test/CIR/Transforms/idiom-recognizer.cpp b/clang/test/CIR/Transforms/idiom-recognizer.cpp index 7264444cd98f..fb00d0611f21 100644 --- a/clang/test/CIR/Transforms/idiom-recognizer.cpp +++ b/clang/test/CIR/Transforms/idiom-recognizer.cpp @@ -18,15 +18,15 @@ int test_find(unsigned char n = 3) // expected-remark@-2 {{found call to end() iterator}} // BEFORE-IDIOM: {{.*}} cir.call @_ZNSt5arrayIhLj9EE5beginEv( - // AFTER-IDIOM: {{.*}} cir.iterator_begin(@_ZNSt5arrayIhLj9EE5beginEv, + // AFTER-IDIOM: {{.*}} cir.std.begin({{.*}}, @_ZNSt5arrayIhLj9EE5beginEv // AFTER-LOWERING-PREPARE: {{.*}} cir.call @_ZNSt5arrayIhLj9EE5beginEv( // BEFORE-IDIOM: {{.*}} cir.call @_ZNSt5arrayIhLj9EE3endEv( - // AFTER-IDIOM: {{.*}} cir.iterator_end(@_ZNSt5arrayIhLj9EE3endEv, + // AFTER-IDIOM: {{.*}} cir.std.end({{.*}}, @_ZNSt5arrayIhLj9EE3endEv // AFTER-LOWERING-PREPARE: {{.*}} cir.call @_ZNSt5arrayIhLj9EE3endEv( // BEFORE-IDIOM: {{.*}} cir.call @_ZSt4findIPhhET_S1_S1_RKT0_( - // AFTER-IDIOM: {{.*}} cir.std.find(@_ZSt4findIPhhET_S1_S1_RKT0_, + // AFTER-IDIOM: {{.*}} cir.std.find({{.*}}, @_ZSt4findIPhhET_S1_S1_RKT0_ // AFTER-LOWERING-PREPARE: {{.*}} cir.call @_ZSt4findIPhhET_S1_S1_RKT0_( if (f != v.end()) // expected-remark {{found call to end() iterator}} @@ -43,8 +43,7 @@ template struct array { }; } -int iter_test() -{ +void iter_test() { yolo::array v = {1, 2, 3}; (void)v.begin(); // no remark should be produced. -} \ No newline at end of file +} From 3cee954f0425524c0a75a7cd3d9c565b34a17933 Mon Sep 17 00:00:00 2001 From: Sirui Mu Date: Fri, 21 Mar 2025 02:23:59 +0800 Subject: [PATCH 05/18] [CIR][NFC] Fix a wrong test case in fc293bb (#1503) --- clang/test/CIR/CodeGen/try-catch.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/test/CIR/CodeGen/try-catch.cpp b/clang/test/CIR/CodeGen/try-catch.cpp index 400a993991e3..7338c0c94e17 100644 --- a/clang/test/CIR/CodeGen/try-catch.cpp +++ b/clang/test/CIR/CodeGen/try-catch.cpp @@ -163,7 +163,7 @@ void tc7() { // CHECK: cir.scope { // CHECK: cir.try { // CHECK: %[[V2:.*]] = cir.load {{.*}} : !cir.ptr, !s32i -// CHECK: %[[V3:.*]] = cir.unary(inc, %[[V2]]) : !s32i, !s32i +// CHECK: %[[V3:.*]] = cir.unary(inc, %[[V2]]) nsw : !s32i, !s32i // CHECK: cir.store %[[V3]], {{.*}} : !s32i, !cir.ptr // CHECK: cir.return // CHECK: } From 23f3d4a39fea590a8189dfa1009f9c3066b99d40 Mon Sep 17 00:00:00 2001 From: Morris Hafner Date: Thu, 20 Mar 2025 19:26:31 +0100 Subject: [PATCH 06/18] cir-translate: Use default target triple instead of x86 if no target was set explicitly (#1482) This is backported from a change made in https://github.com/llvm/llvm-project/pull/131181 --------- Co-authored-by: Morris Hafner --- clang/test/CIR/Lowering/OpenMP/barrier.cir | 2 +- clang/test/CIR/Lowering/OpenMP/parallel.cir | 2 +- clang/test/CIR/Lowering/OpenMP/taskwait.cir | 2 +- clang/test/CIR/Lowering/OpenMP/taskyield.cir | 2 +- clang/test/CIR/Lowering/array.cir | 2 +- clang/test/CIR/Lowering/attribute-lowering.cir | 2 +- clang/test/CIR/Lowering/binop-fp.cir | 2 +- clang/test/CIR/Lowering/binop-overflow.cir | 2 +- clang/test/CIR/Lowering/binop-unsigned-int.cir | 2 +- clang/test/CIR/Lowering/bitint.cir | 2 +- clang/test/CIR/Lowering/bool-to-int.cir | 2 +- clang/test/CIR/Lowering/bool.cir | 2 +- clang/test/CIR/Lowering/branch.cir | 2 +- clang/test/CIR/Lowering/brcond.cir | 2 +- clang/test/CIR/Lowering/bswap.cir | 2 +- clang/test/CIR/Lowering/call-op-call-conv.cir | 2 +- clang/test/CIR/Lowering/call.cir | 2 +- clang/test/CIR/Lowering/cmp3way.cir | 2 +- clang/test/CIR/Lowering/complex.cir | 2 +- clang/test/CIR/Lowering/const-array.cir | 2 +- clang/test/CIR/Lowering/expect.cir | 2 +- clang/test/CIR/Lowering/func-call-conv.cir | 2 +- clang/test/CIR/Lowering/globals.cir | 2 +- clang/test/CIR/Lowering/if.cir | 2 +- clang/test/CIR/Lowering/int-wrap.cir | 2 +- clang/test/CIR/Lowering/intrinsics.cir | 2 +- clang/test/CIR/Lowering/ptrdiff.cir | 2 +- clang/test/CIR/Lowering/region-simplify.cir | 2 +- clang/test/CIR/Lowering/scope.cir | 2 +- clang/test/CIR/Lowering/select.cir | 2 +- clang/test/CIR/Lowering/syncscope.cir | 2 +- clang/test/CIR/Lowering/unary-inc-dec.cir | 2 +- clang/test/CIR/Lowering/unary-not.cir | 2 +- .../CIR/Tools/cir-translate/no-triple-has-data-layout.cir | 5 ----- .../CIR/Tools/cir-translate/no-triple-no-data-layout.cir | 5 ----- clang/test/CIR/Tools/cir-translate/warn-default-triple.cir | 5 +++-- clang/tools/cir-translate/cir-translate.cpp | 4 +--- 37 files changed, 37 insertions(+), 48 deletions(-) diff --git a/clang/test/CIR/Lowering/OpenMP/barrier.cir b/clang/test/CIR/Lowering/OpenMP/barrier.cir index 145117ab54a0..c6a58bb02038 100644 --- a/clang/test/CIR/Lowering/OpenMP/barrier.cir +++ b/clang/test/CIR/Lowering/OpenMP/barrier.cir @@ -1,5 +1,5 @@ -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s module { diff --git a/clang/test/CIR/Lowering/OpenMP/parallel.cir b/clang/test/CIR/Lowering/OpenMP/parallel.cir index 3422eac75ea0..dd1f0c763053 100644 --- a/clang/test/CIR/Lowering/OpenMP/parallel.cir +++ b/clang/test/CIR/Lowering/OpenMP/parallel.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s !s32i = !cir.int module { diff --git a/clang/test/CIR/Lowering/OpenMP/taskwait.cir b/clang/test/CIR/Lowering/OpenMP/taskwait.cir index 83e8119bc479..bb4f4fff4392 100644 --- a/clang/test/CIR/Lowering/OpenMP/taskwait.cir +++ b/clang/test/CIR/Lowering/OpenMP/taskwait.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s module { diff --git a/clang/test/CIR/Lowering/OpenMP/taskyield.cir b/clang/test/CIR/Lowering/OpenMP/taskyield.cir index a701365b798f..63a525d9f75c 100644 --- a/clang/test/CIR/Lowering/OpenMP/taskyield.cir +++ b/clang/test/CIR/Lowering/OpenMP/taskyield.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s module { diff --git a/clang/test/CIR/Lowering/array.cir b/clang/test/CIR/Lowering/array.cir index 30a5aae7bfae..09830b64e5ff 100644 --- a/clang/test/CIR/Lowering/array.cir +++ b/clang/test/CIR/Lowering/array.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM !s32i = !cir.int !ty_S = !cir.struct diff --git a/clang/test/CIR/Lowering/attribute-lowering.cir b/clang/test/CIR/Lowering/attribute-lowering.cir index 71ddf2002a25..3d2e2bd08e47 100644 --- a/clang/test/CIR/Lowering/attribute-lowering.cir +++ b/clang/test/CIR/Lowering/attribute-lowering.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM !s32i = !cir.int !s8i = !cir.int diff --git a/clang/test/CIR/Lowering/binop-fp.cir b/clang/test/CIR/Lowering/binop-fp.cir index a2800a847c85..e69a69e6b099 100644 --- a/clang/test/CIR/Lowering/binop-fp.cir +++ b/clang/test/CIR/Lowering/binop-fp.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM module { cir.func @foo() { diff --git a/clang/test/CIR/Lowering/binop-overflow.cir b/clang/test/CIR/Lowering/binop-overflow.cir index 6a2ef54c1501..68af70aa6abb 100644 --- a/clang/test/CIR/Lowering/binop-overflow.cir +++ b/clang/test/CIR/Lowering/binop-overflow.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM !u32i = !cir.int !s32i = !cir.int diff --git a/clang/test/CIR/Lowering/binop-unsigned-int.cir b/clang/test/CIR/Lowering/binop-unsigned-int.cir index b783509d06ed..8834cc103e5a 100644 --- a/clang/test/CIR/Lowering/binop-unsigned-int.cir +++ b/clang/test/CIR/Lowering/binop-unsigned-int.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !u32i = !cir.int module { diff --git a/clang/test/CIR/Lowering/bitint.cir b/clang/test/CIR/Lowering/bitint.cir index 61db545b0d07..ea6dcf989aa4 100644 --- a/clang/test/CIR/Lowering/bitint.cir +++ b/clang/test/CIR/Lowering/bitint.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s32i = !cir.int diff --git a/clang/test/CIR/Lowering/bool-to-int.cir b/clang/test/CIR/Lowering/bool-to-int.cir index 97ee3c1daee0..772e5c5c4e45 100644 --- a/clang/test/CIR/Lowering/bool-to-int.cir +++ b/clang/test/CIR/Lowering/bool-to-int.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s !s32i = !cir.int #false = #cir.bool : !cir.bool diff --git a/clang/test/CIR/Lowering/bool.cir b/clang/test/CIR/Lowering/bool.cir index 848b552f897a..4398c60cc6ed 100644 --- a/clang/test/CIR/Lowering/bool.cir +++ b/clang/test/CIR/Lowering/bool.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM #false = #cir.bool : !cir.bool #true = #cir.bool : !cir.bool diff --git a/clang/test/CIR/Lowering/branch.cir b/clang/test/CIR/Lowering/branch.cir index 0daea329f4b8..ed8eea362039 100644 --- a/clang/test/CIR/Lowering/branch.cir +++ b/clang/test/CIR/Lowering/branch.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s32i = !cir.int cir.func @foo(%arg0: !cir.bool) -> !s32i { diff --git a/clang/test/CIR/Lowering/brcond.cir b/clang/test/CIR/Lowering/brcond.cir index 19e778cef823..bacddbcdaa0f 100644 --- a/clang/test/CIR/Lowering/brcond.cir +++ b/clang/test/CIR/Lowering/brcond.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s32i = !cir.int #fn_attr = #cir, nothrow = #cir.nothrow, optnone = #cir.optnone})> diff --git a/clang/test/CIR/Lowering/bswap.cir b/clang/test/CIR/Lowering/bswap.cir index 0f8478ba8936..dc3a0744d0be 100644 --- a/clang/test/CIR/Lowering/bswap.cir +++ b/clang/test/CIR/Lowering/bswap.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !u32i = !cir.int diff --git a/clang/test/CIR/Lowering/call-op-call-conv.cir b/clang/test/CIR/Lowering/call-op-call-conv.cir index 92f0028e7bae..d4360158575f 100644 --- a/clang/test/CIR/Lowering/call-op-call-conv.cir +++ b/clang/test/CIR/Lowering/call-op-call-conv.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate -cir-to-llvmir --disable-cc-lowering %s -o %t.ll +// RUN: cir-translate -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering %s -o %t.ll // RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM !s32i = !cir.int diff --git a/clang/test/CIR/Lowering/call.cir b/clang/test/CIR/Lowering/call.cir index 07957727e861..1b57a9fd92df 100644 --- a/clang/test/CIR/Lowering/call.cir +++ b/clang/test/CIR/Lowering/call.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM // XFAIL: * !s32i = !cir.int diff --git a/clang/test/CIR/Lowering/cmp3way.cir b/clang/test/CIR/Lowering/cmp3way.cir index 9c18dfce5769..3eda6dd9fcc9 100644 --- a/clang/test/CIR/Lowering/cmp3way.cir +++ b/clang/test/CIR/Lowering/cmp3way.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s8i = !cir.int !s32i = !cir.int diff --git a/clang/test/CIR/Lowering/complex.cir b/clang/test/CIR/Lowering/complex.cir index 27180865e377..40cbf63cc2fb 100644 --- a/clang/test/CIR/Lowering/complex.cir +++ b/clang/test/CIR/Lowering/complex.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate -cir-to-llvmir --disable-cc-lowering -o %t.ll %s +// RUN: cir-translate -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o %t.ll %s // RUN: FileCheck --input-file %t.ll -check-prefix=LLVM %s !s32i = !cir.int diff --git a/clang/test/CIR/Lowering/const-array.cir b/clang/test/CIR/Lowering/const-array.cir index 84a21665bffd..cf16cca3f5b1 100644 --- a/clang/test/CIR/Lowering/const-array.cir +++ b/clang/test/CIR/Lowering/const-array.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM !u8i = !cir.int #false = #cir.bool : !cir.bool diff --git a/clang/test/CIR/Lowering/expect.cir b/clang/test/CIR/Lowering/expect.cir index 57f9cf2e35da..aa300745d890 100644 --- a/clang/test/CIR/Lowering/expect.cir +++ b/clang/test/CIR/Lowering/expect.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s64i = !cir.int module { diff --git a/clang/test/CIR/Lowering/func-call-conv.cir b/clang/test/CIR/Lowering/func-call-conv.cir index 577eb854d47b..cf73d0bae285 100644 --- a/clang/test/CIR/Lowering/func-call-conv.cir +++ b/clang/test/CIR/Lowering/func-call-conv.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o %t.ll +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o %t.ll // RUN: FileCheck %s --input-file=%t.ll --check-prefix=LLVM !s32i = !cir.int diff --git a/clang/test/CIR/Lowering/globals.cir b/clang/test/CIR/Lowering/globals.cir index 3f99fd102efd..58a28490d007 100644 --- a/clang/test/CIR/Lowering/globals.cir +++ b/clang/test/CIR/Lowering/globals.cir @@ -1,6 +1,6 @@ // RUN: cir-opt %s -cir-to-llvm -o %t.mlir // RUN: FileCheck --input-file=%t.mlir %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o %t.ll +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o %t.ll // RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM !void = !cir.void diff --git a/clang/test/CIR/Lowering/if.cir b/clang/test/CIR/Lowering/if.cir index 44aa412ffd13..3a077aa9ef05 100644 --- a/clang/test/CIR/Lowering/if.cir +++ b/clang/test/CIR/Lowering/if.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s32i = !cir.int module { diff --git a/clang/test/CIR/Lowering/int-wrap.cir b/clang/test/CIR/Lowering/int-wrap.cir index f885e745004b..b5fb79ba69ed 100644 --- a/clang/test/CIR/Lowering/int-wrap.cir +++ b/clang/test/CIR/Lowering/int-wrap.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s32i = !cir.int module { diff --git a/clang/test/CIR/Lowering/intrinsics.cir b/clang/test/CIR/Lowering/intrinsics.cir index 778aeb9f9182..e5ac27d89bfc 100644 --- a/clang/test/CIR/Lowering/intrinsics.cir +++ b/clang/test/CIR/Lowering/intrinsics.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM module { cir.func @test_unreachable() { diff --git a/clang/test/CIR/Lowering/ptrdiff.cir b/clang/test/CIR/Lowering/ptrdiff.cir index c0b1a4b3e314..a572367489d9 100644 --- a/clang/test/CIR/Lowering/ptrdiff.cir +++ b/clang/test/CIR/Lowering/ptrdiff.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s !s32i = !cir.int !u64i = !cir.int diff --git a/clang/test/CIR/Lowering/region-simplify.cir b/clang/test/CIR/Lowering/region-simplify.cir index a76d73d03d8e..8f1589eea9f9 100644 --- a/clang/test/CIR/Lowering/region-simplify.cir +++ b/clang/test/CIR/Lowering/region-simplify.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -canonicalize -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-opt %s -canonicalize -o - | cir-translate -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-opt %s -canonicalize -o - | cir-translate -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !u32i = !cir.int diff --git a/clang/test/CIR/Lowering/scope.cir b/clang/test/CIR/Lowering/scope.cir index 850b1ec5e051..092c551a9fd6 100644 --- a/clang/test/CIR/Lowering/scope.cir +++ b/clang/test/CIR/Lowering/scope.cir @@ -1,6 +1,6 @@ // RUN: cir-opt %s -cir-to-llvm -o %t.cir // RUN: FileCheck %s --input-file=%t.cir -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !u32i = !cir.int module { diff --git a/clang/test/CIR/Lowering/select.cir b/clang/test/CIR/Lowering/select.cir index 71ca79a390e8..5067355ca8f6 100644 --- a/clang/test/CIR/Lowering/select.cir +++ b/clang/test/CIR/Lowering/select.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate -cir-to-llvmir --disable-cc-lowering -o %t.ll %s +// RUN: cir-translate -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o %t.ll %s // RUN: FileCheck --input-file=%t.ll -check-prefix=LLVM %s !s32i = !cir.int diff --git a/clang/test/CIR/Lowering/syncscope.cir b/clang/test/CIR/Lowering/syncscope.cir index 8e1aad8a743c..fe324bba3524 100644 --- a/clang/test/CIR/Lowering/syncscope.cir +++ b/clang/test/CIR/Lowering/syncscope.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM !s32i = !cir.int #fn_attr = #cir, nothrow = #cir.nothrow, optnone = #cir.optnone})> diff --git a/clang/test/CIR/Lowering/unary-inc-dec.cir b/clang/test/CIR/Lowering/unary-inc-dec.cir index 4dac6ac55318..46d2f1115c59 100644 --- a/clang/test/CIR/Lowering/unary-inc-dec.cir +++ b/clang/test/CIR/Lowering/unary-inc-dec.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s32i = !cir.int module { cir.func @foo() { diff --git a/clang/test/CIR/Lowering/unary-not.cir b/clang/test/CIR/Lowering/unary-not.cir index 35cd54f3df78..1ad25d790f35 100644 --- a/clang/test/CIR/Lowering/unary-not.cir +++ b/clang/test/CIR/Lowering/unary-not.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s32i = !cir.int module { cir.func @foo() -> !s32i { diff --git a/clang/test/CIR/Tools/cir-translate/no-triple-has-data-layout.cir b/clang/test/CIR/Tools/cir-translate/no-triple-has-data-layout.cir index f2853941271f..5dc2fdb75a7c 100644 --- a/clang/test/CIR/Tools/cir-translate/no-triple-has-data-layout.cir +++ b/clang/test/CIR/Tools/cir-translate/no-triple-has-data-layout.cir @@ -2,8 +2,6 @@ // RUN: FileCheck %s -input-file %t.x86.ll -check-prefix=X86 // RUN: cir-translate --cir-to-llvmir --target spirv64-unknown-unknown --disable-cc-lowering %s -o %t.spirv64.ll // RUN: FileCheck %s -input-file %t.spirv64.ll -check-prefix=SPIRV64 -// RUN: cir-translate --cir-to-llvmir --disable-cc-lowering %s -o %t.default.ll -// RUN: FileCheck %s -input-file %t.default.ll -check-prefix=DEFAULT module attributes { dlti.dl_spec = #dlti.dl_spec<"dlti.global_memory_space" = 7 : ui64> @@ -18,6 +16,3 @@ module attributes { // SPIRV64-NOT: target datalayout = "G7" // SPIRV64-DAG: target triple = "spirv64-unknown-unknown" - -// DEFAULT-NOT: target datalayout = "G7" -// DEFAULT-DAG: target triple = "x86_64-unknown-linux-gnu" diff --git a/clang/test/CIR/Tools/cir-translate/no-triple-no-data-layout.cir b/clang/test/CIR/Tools/cir-translate/no-triple-no-data-layout.cir index f18f69dd876d..2e336a797c6a 100644 --- a/clang/test/CIR/Tools/cir-translate/no-triple-no-data-layout.cir +++ b/clang/test/CIR/Tools/cir-translate/no-triple-no-data-layout.cir @@ -2,8 +2,6 @@ // RUN: FileCheck %s -input-file %t.x86.ll -check-prefix=X86 // RUN: cir-translate --cir-to-llvmir --target spirv64-unknown-unknown --disable-cc-lowering %s -o %t.spirv64.ll // RUN: FileCheck %s -input-file %t.spirv64.ll -check-prefix=SPIRV64 -// RUN: cir-translate --cir-to-llvmir --disable-cc-lowering %s -o %t.default.ll -// RUN: FileCheck %s -input-file %t.default.ll -check-prefix=DEFAULT module { cir.func @foo() { @@ -16,6 +14,3 @@ module { // SPIRV64-DAG: target triple = "spirv64-unknown-unknown" // SPIRV64-DAG: target datalayout = "{{.*}}" - -// DEFAULT-DAG: target triple = "x86_64-unknown-linux-gnu" -// DEFAULT-DAG: target datalayout = "{{.*}}" diff --git a/clang/test/CIR/Tools/cir-translate/warn-default-triple.cir b/clang/test/CIR/Tools/cir-translate/warn-default-triple.cir index 519e96598d43..9d60324905f7 100644 --- a/clang/test/CIR/Tools/cir-translate/warn-default-triple.cir +++ b/clang/test/CIR/Tools/cir-translate/warn-default-triple.cir @@ -1,6 +1,7 @@ -// RUN: cir-translate -verify-diagnostics --cir-to-llvmir --disable-cc-lowering %s +// XFAIL: target={{.*windows.*}} +// RUN: cir-translate -verify-diagnostics --cir-to-llvmir --disable-cc-lowering %s 2>&1 -// expected-warning@below {{no target triple provided, assuming x86_64-unknown-linux-gnu}} +// expected-warning@below {{no target triple provided, assuming}} module { cir.func @foo() { cir.return diff --git a/clang/tools/cir-translate/cir-translate.cpp b/clang/tools/cir-translate/cir-translate.cpp index bc8fea1f635e..d0e33b45ccf4 100644 --- a/clang/tools/cir-translate/cir-translate.cpp +++ b/clang/tools/cir-translate/cir-translate.cpp @@ -69,9 +69,7 @@ std::string prepareCIRModuleTriple(mlir::ModuleOp mod) { // Treat "" as the default target machine. if (triple.empty()) { - // Currently ClangIR only supports a couple of targets. Not specifying a - // target triple will default to x86_64-unknown-linux-gnu. - triple = "x86_64-unknown-linux-gnu"; + triple = llvm::sys::getDefaultTargetTriple(); mod.emitWarning() << "no target triple provided, assuming " << triple; } From 581d7e633c03cd70f4b430887de74e92d4f6e042 Mon Sep 17 00:00:00 2001 From: Sharp-Edged <48861530+Sharp-Edged@users.noreply.github.com> Date: Thu, 20 Mar 2025 18:27:37 +0000 Subject: [PATCH 07/18] [CIR][CUDA] Decorate global CUDA shadow variables with appropriate CIR attribute. (#1467) Started decorating CUDA shadow variables with the shadow_name CIR attribute which will be used for registering the globals. --- clang/lib/CIR/CodeGen/CIRGenCUDARuntime.cpp | 53 ++++++++++++++++++++- clang/lib/CIR/CodeGen/CIRGenCUDARuntime.h | 6 +++ clang/lib/CIR/CodeGen/CIRGenModule.cpp | 45 +++++++++++++++++ clang/lib/CIR/CodeGen/CIRGenModule.h | 8 ++++ clang/test/CIR/CodeGen/CUDA/global-vars.cu | 7 +++ 5 files changed, 118 insertions(+), 1 deletion(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenCUDARuntime.cpp b/clang/lib/CIR/CodeGen/CIRGenCUDARuntime.cpp index 2695e731b061..71041f1493cc 100644 --- a/clang/lib/CIR/CodeGen/CIRGenCUDARuntime.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenCUDARuntime.cpp @@ -13,6 +13,7 @@ //===----------------------------------------------------------------------===// #include "CIRGenCUDARuntime.h" +#include "CIRGenCXXABI.h" #include "CIRGenFunction.h" #include "mlir/IR/Operation.h" #include "clang/Basic/Cuda.h" @@ -23,9 +24,26 @@ using namespace clang; using namespace clang::CIRGen; +static std::unique_ptr initDeviceMC(CIRGenModule &cgm) { + // If the host and device have different C++ ABIs, mark it as the device + // mangle context so that the mangling needs to retrieve the additional + // device lambda mangling number instead of the regular host one. + if (cgm.getASTContext().getAuxTargetInfo() && + cgm.getASTContext().getTargetInfo().getCXXABI().isMicrosoft() && + cgm.getASTContext().getAuxTargetInfo()->getCXXABI().isItaniumFamily()) { + return std::unique_ptr( + cgm.getASTContext().createDeviceMangleContext( + *cgm.getASTContext().getAuxTargetInfo())); + } + + return std::unique_ptr(cgm.getASTContext().createMangleContext( + cgm.getASTContext().getAuxTargetInfo())); +} + CIRGenCUDARuntime::~CIRGenCUDARuntime() {} -CIRGenCUDARuntime::CIRGenCUDARuntime(CIRGenModule &cgm) : cgm(cgm) { +CIRGenCUDARuntime::CIRGenCUDARuntime(CIRGenModule &cgm) + : cgm(cgm), deviceMC(initDeviceMC(cgm)) { if (cgm.getLangOpts().OffloadViaLLVM) llvm_unreachable("NYI"); else if (cgm.getLangOpts().HIP) @@ -289,6 +307,39 @@ mlir::Operation *CIRGenCUDARuntime::getKernelHandle(cir::FuncOp fn, return globalOp; } +std::string CIRGenCUDARuntime::getDeviceSideName(const NamedDecl *nd) { + GlobalDecl gd; + // nd could be either a kernel or a variable. + if (auto *fd = dyn_cast(nd)) + gd = GlobalDecl(fd, KernelReferenceKind::Kernel); + else + gd = GlobalDecl(nd); + std::string deviceSideName; + MangleContext *mc; + if (cgm.getLangOpts().CUDAIsDevice) + mc = &cgm.getCXXABI().getMangleContext(); + else + mc = deviceMC.get(); + if (mc->shouldMangleDeclName(nd)) { + SmallString<256> buffer; + llvm::raw_svector_ostream out(buffer); + mc->mangleName(gd, out); + deviceSideName = std::string(out.str()); + } else + deviceSideName = std::string(nd->getIdentifier()->getName()); + + // Make unique name for device side static file-scope variable for HIP. + if (cgm.getASTContext().shouldExternalize(nd) && + cgm.getLangOpts().GPURelocatableDeviceCode) { + SmallString<256> buffer; + llvm::raw_svector_ostream out(buffer); + out << deviceSideName; + cgm.printPostfixForExternalizedDecl(out, nd); + deviceSideName = std::string(out.str()); + } + return deviceSideName; +} + void CIRGenCUDARuntime::internalizeDeviceSideVar( const VarDecl *d, cir::GlobalLinkageKind &linkage) { if (cgm.getLangOpts().GPURelocatableDeviceCode) diff --git a/clang/lib/CIR/CodeGen/CIRGenCUDARuntime.h b/clang/lib/CIR/CodeGen/CIRGenCUDARuntime.h index 8cbadb849129..84edcb6b7a3e 100644 --- a/clang/lib/CIR/CodeGen/CIRGenCUDARuntime.h +++ b/clang/lib/CIR/CodeGen/CIRGenCUDARuntime.h @@ -47,6 +47,9 @@ class CIRGenCUDARuntime { std::string addPrefixToName(StringRef FuncName) const; std::string addUnderscoredPrefixToName(StringRef FuncName) const; + // Mangle context for device. + std::unique_ptr deviceMC; + public: CIRGenCUDARuntime(CIRGenModule &cgm); virtual ~CIRGenCUDARuntime(); @@ -60,6 +63,9 @@ class CIRGenCUDARuntime { virtual mlir::Operation *getKernelHandle(cir::FuncOp fn, GlobalDecl GD); virtual void internalizeDeviceSideVar(const VarDecl *d, cir::GlobalLinkageKind &linkage); + /// Returns function or variable name on device side even if the current + /// compilation is for host. + virtual std::string getDeviceSideName(const NamedDecl *nd); }; } // namespace clang::CIRGen diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 6c2c907f0573..d83a00626967 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -576,6 +576,25 @@ bool CIRGenModule::shouldEmitCUDAGlobalVar(const VarDecl *global) const { global->getType()->isCUDADeviceBuiltinTextureType(); } +void CIRGenModule::printPostfixForExternalizedDecl(llvm::raw_ostream &os, + const Decl *d) const { + // ptxas does not allow '.' in symbol names. On the other hand, HIP prefers + // postfix beginning with '.' since the symbol name can be demangled. + if (langOpts.HIP) + os << (isa(d) ? ".static." : ".intern."); + else + os << (isa(d) ? "__static__" : "__intern__"); + + // If the CUID is not specified we try to generate a unique postfix. + if (getLangOpts().CUID.empty()) { + // TODO: Once we add 'PreprocessorOpts' into CIRGenModule this part can be + // brought in from OG. + llvm_unreachable("NYI"); + } else { + os << getASTContext().getCUIDHash(); + } +} + void CIRGenModule::emitGlobal(GlobalDecl gd) { llvm::TimeTraceScope scope("build CIR Global", [&]() -> std::string { auto *nd = dyn_cast(gd.getDecl()); @@ -1496,6 +1515,32 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *d, } } + // Decorate CUDA shadow variables with the cu.shadow_name attribute so we know + // how to register them when lowering. + if (langOpts.CUDA && !langOpts.CUDAIsDevice && + (d->hasAttr() || d->hasAttr())) { + // Shadow variables and their properties must be registered with CUDA + // runtime. Skip Extern global variables, which will be registered in + // the TU where they are defined. + // + // Don't register a C++17 inline variable. The local symbol can be + // discarded and referencing a discarded local symbol from outside the + // comdat (__cuda_register_globals) is disallowed by the ELF spec. + // + // HIP managed variables need to be always recorded in device and host + // compilations for transformation. + // + // HIP managed variables and variables in CUDADeviceVarODRUsedByHost are + // added to llvm.compiler-used, therefore they are safe to be registered. + if ((!d->hasExternalStorage() && !d->isInline()) || + getASTContext().CUDADeviceVarODRUsedByHost.contains(d) || + d->hasAttr()) { + auto shadowName = cudaRuntime->getDeviceSideName(cast(d)); + auto attr = CUDAShadowNameAttr::get(&getMLIRContext(), shadowName); + gv->setAttr(CUDAShadowNameAttr::getMnemonic(), attr); + } + } + // Set initializer and finalize emission CIRGenModule::setInitializer(gv, init); if (emitter) diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.h b/clang/lib/CIR/CodeGen/CIRGenModule.h index a4c593e86ee9..b3e5e104cf43 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.h +++ b/clang/lib/CIR/CodeGen/CIRGenModule.h @@ -274,6 +274,14 @@ class CIRGenModule : public CIRGenTypeCache { bool shouldEmitCUDAGlobalVar(const VarDecl *global) const; + /// Print the postfix for externalized static variable or kernels for single + /// source offloading languages CUDA and HIP. The unique postfix is created + /// using either the CUID argument, or the file's UniqueID and active macros. + /// The fallback method without a CUID requires that the offloading toolchain + /// does not define separate macros via the -cc1 options. + void printPostfixForExternalizedDecl(llvm::raw_ostream &OS, + const Decl *D) const; + bool shouldEmitConvergenceTokens() const { // TODO: this shuld probably become unconditional once the controlled // convergence becomes the norm. diff --git a/clang/test/CIR/CodeGen/CUDA/global-vars.cu b/clang/test/CIR/CodeGen/CUDA/global-vars.cu index 25de5f28dd91..55d4c67967fa 100644 --- a/clang/test/CIR/CodeGen/CUDA/global-vars.cu +++ b/clang/test/CIR/CodeGen/CUDA/global-vars.cu @@ -10,9 +10,15 @@ // RUN: %s -o %t.cir // RUN: FileCheck --check-prefix=LLVM-DEVICE --input-file=%t.cir %s +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir \ +// RUN: -x cuda -emit-cir -target-sdk-version=12.3 \ +// RUN: %s -o %t.cir +// RUN: FileCheck --check-prefix=CIR-HOST --input-file=%t.cir %s + __device__ int a; // CIR-DEVICE: cir.global external addrspace(offload_global) @a = #cir.int<0> // LLVM-DEVICE: @a = addrspace(1) externally_initialized global i32 0, align 4 +// CIR-HOST: {{.*}}cir.global external @a = #cir.undef : !s32i {alignment = 4 : i64, cu.shadow_name = #cir.cu.shadow_name}{{.*}} __shared__ int shared; // CIR-DEVICE: cir.global external addrspace(offload_local) @shared = #cir.undef @@ -21,3 +27,4 @@ __shared__ int shared; __constant__ int b; // CIR-DEVICE: cir.global constant external addrspace(offload_constant) @b = #cir.int<0> : !s32i {alignment = 4 : i64, cu.externally_initialized = #cir.cu.externally_initialized} // LLVM-DEVICE: @b = addrspace(4) externally_initialized constant i32 0, align 4 +// CIR-HOST: {{.*}}cir.global external @b = #cir.undef : !s32i {alignment = 4 : i64, cu.shadow_name = #cir.cu.shadow_name}{{.*}} From 70b79ee0b2ec8136fd090b06874a5e000a5d2a24 Mon Sep 17 00:00:00 2001 From: Iris <0.0@owo.li> Date: Fri, 21 Mar 2025 02:28:25 +0800 Subject: [PATCH 08/18] [CIR][CIRGen][Builtin] add `__builtin_tan` (#1502) Closes #1354. --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 1 + clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 3 +- .../test/CIR/CodeGen/builtin-floating-point.c | 68 +++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 3f00dda0f904..830da08a27fe 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -4752,6 +4752,7 @@ def RoundOp : UnaryFPToFPBuiltinOp<"round", "RoundOp">; def RoundEvenOp : UnaryFPToFPBuiltinOp<"roundeven", "RoundEvenOp">; def SinOp : UnaryFPToFPBuiltinOp<"sin", "SinOp">; def SqrtOp : UnaryFPToFPBuiltinOp<"sqrt", "SqrtOp">; +def TanOp : UnaryFPToFPBuiltinOp<"tan", "TanOp">; def TruncOp : UnaryFPToFPBuiltinOp<"trunc", "FTruncOp">; def AbsOp : CIR_Op<"abs", [Pure, SameOperandsAndResultType]> { diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index 78d96c39396b..075c951f1086 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -896,7 +896,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, case Builtin::BI__builtin_tanf16: case Builtin::BI__builtin_tanl: case Builtin::BI__builtin_tanf128: - llvm_unreachable("Builtin::BItan like NYI"); + assert(!cir::MissingFeatures::fastMathFlags()); + return emitUnaryMaybeConstrainedFPBuiltin(*this, *E); case Builtin::BItanh: case Builtin::BItanhf: diff --git a/clang/test/CIR/CodeGen/builtin-floating-point.c b/clang/test/CIR/CodeGen/builtin-floating-point.c index 2e9b18c51a33..8af6fd7b4ed4 100644 --- a/clang/test/CIR/CodeGen/builtin-floating-point.c +++ b/clang/test/CIR/CodeGen/builtin-floating-point.c @@ -1159,6 +1159,74 @@ long double call_sqrtl(long double f) { // LLVM: } } +// tan + +float my_tanf(float f) { + return __builtin_tanf(f); + // CHECK: cir.func @my_tanf + // CHECK: {{.+}} = cir.tan {{.+}} : !cir.float + + // LLVM: define dso_local float @my_tanf(float %0) + // LLVM: %{{.+}} = call float @llvm.tan.f32(float %{{.+}}) + // LLVM: } +} + +double my_tan(double f) { + return __builtin_tan(f); + // CHECK: cir.func @my_tan + // CHECK: {{.+}} = cir.tan {{.+}} : !cir.double + + // LLVM: define dso_local double @my_tan(double %0) + // LLVM: %{{.+}} = call double @llvm.tan.f64(double %{{.+}}) + // LLVM: } +} + +long double my_tanl(long double f) { + return __builtin_tanl(f); + // CHECK: cir.func @my_tanl + // CHECK: {{.+}} = cir.tan {{.+}} : !cir.long_double + // AARCH64: {{.+}} = cir.tan {{.+}} : !cir.long_double + + // LLVM: define dso_local x86_fp80 @my_tanl(x86_fp80 %0) + // LLVM: %{{.+}} = call x86_fp80 @llvm.tan.f80(x86_fp80 %{{.+}}) + // LLVM: } +} + +float tanf(float); +double tan(double); +long double tanl(long double); + +float call_tanf(float f) { + return tanf(f); + // CHECK: cir.func @call_tanf + // CHECK: {{.+}} = cir.tan {{.+}} : !cir.float + + // LLVM: define dso_local float @call_tanf(float %0) + // LLVM: %{{.+}} = call float @llvm.tan.f32(float %{{.+}}) + // LLVM: } +} + +double call_tan(double f) { + return tan(f); + // CHECK: cir.func @call_tan + // CHECK: {{.+}} = cir.tan {{.+}} : !cir.double + + // LLVM: define dso_local double @call_tan(double %0) + // LLVM: %{{.+}} = call double @llvm.tan.f64(double %{{.+}}) + // LLVM: } +} + +long double call_tanl(long double f) { + return tanl(f); + // CHECK: cir.func @call_tanl + // CHECK: {{.+}} = cir.tan {{.+}} : !cir.long_double + // AARCH64: {{.+}} = cir.tan {{.+}} : !cir.long_double + + // LLVM: define dso_local x86_fp80 @call_tanl(x86_fp80 %0) + // LLVM: %{{.+}} = call x86_fp80 @llvm.tan.f80(x86_fp80 %{{.+}}) + // LLVM: } +} + // trunc float my_truncf(float f) { From b0887ead07788366c403d655de12e939c135b62d Mon Sep 17 00:00:00 2001 From: Amr Hesham Date: Thu, 20 Mar 2025 19:31:07 +0100 Subject: [PATCH 09/18] [CIR][CIRGen][Builtin][Neon] Lower neon vcaltd_f64 (#1505) Lower neon vcaltd_f64 --- clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp | 3 ++- clang/test/CIR/CodeGen/AArch64/neon.c | 16 ++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp index ef09403264ac..a3d2bed57fa4 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp @@ -2713,7 +2713,8 @@ static mlir::Value emitCommonNeonSISDBuiltinExpr( return emitNeonCall(builder, {argTy}, ops, "aarch64.neon.facge", resultTy, loc); case NEON::BI__builtin_neon_vcaltd_f64: - llvm_unreachable(" neon_vcaltd_f64 NYI "); + return emitNeonCall(builder, {argTy}, ops, "aarch64.neon.facgt", resultTy, + loc); case NEON::BI__builtin_neon_vcalts_f32: llvm_unreachable(" neon_vcalts_f32 NYI "); case NEON::BI__builtin_neon_vcvtad_s64_f64: diff --git a/clang/test/CIR/CodeGen/AArch64/neon.c b/clang/test/CIR/CodeGen/AArch64/neon.c index 6419c43e4baa..a634af0f1f4e 100644 --- a/clang/test/CIR/CodeGen/AArch64/neon.c +++ b/clang/test/CIR/CodeGen/AArch64/neon.c @@ -15185,12 +15185,16 @@ uint64_t test_vcaled_f64(float64_t a, float64_t b) { // return (uint32_t)vcalts_f32(a, b); // } -// NYI-LABEL: @test_vcaltd_f64( -// NYI: [[VCALTD_F64_I:%.*]] = call i64 @llvm.aarch64.neon.facgt.i64.f64(double %b, double %a) -// NYI: ret i64 [[VCALTD_F64_I]] -// uint64_t test_vcaltd_f64(float64_t a, float64_t b) { -// return (uint64_t)vcaltd_f64(a, b); -// } +uint64_t test_vcaltd_f64(float64_t a, float64_t b) { + return (uint64_t)vcaltd_f64(a, b); + + // CIR-LABEL: vcaltd_f64 + // CIR: [[TMP0:%.*]] = cir.llvm.intrinsic "aarch64.neon.facgt" {{.*}}, {{.*}} : (!cir.double, !cir.double) -> !u64i + + // LLVM-LABEL: @test_vcaltd_f64( + // LLVM: [[VCALTD_F64_I:%.*]] = call i64 @llvm.aarch64.neon.facgt.i64.f64(double %0, double %1) + // LLVM: ret i64 [[VCALTD_F64_I]] +} int64_t test_vshrd_n_s64(int64_t a) { return (int64_t)vshrd_n_s64(a, 1); From 658a756715db6cfc8d024db3eed9d9c3c4aa721f Mon Sep 17 00:00:00 2001 From: Bruno Cardoso Lopes Date: Thu, 20 Mar 2025 14:46:06 -0700 Subject: [PATCH 10/18] Revert "cir-translate: Use default target triple instead of x86 if no target was set explicitly" (#1509) Reverts llvm/clangir#1482 @mmha this is crashing on macos on asserts build: ``` FAIL: Clang :: CIR/Tools/cir-translate/warn-default-triple.cir (472 of 552) ******************** TEST 'Clang :: CIR/Tools/cir-translate/warn-default-triple.cir' FAILED ******************** Exit Code: 134 Command Output (stdout): -- Assertion failed: (!DataLayoutString.empty() && "Uninitialized DataLayout!"), function getDataLayoutString, file TargetInfo.h, line 1282. ``` Perhaps besides picking a default you maybe need to do some missing datalayout init? --- clang/test/CIR/Lowering/OpenMP/barrier.cir | 2 +- clang/test/CIR/Lowering/OpenMP/parallel.cir | 2 +- clang/test/CIR/Lowering/OpenMP/taskwait.cir | 2 +- clang/test/CIR/Lowering/OpenMP/taskyield.cir | 2 +- clang/test/CIR/Lowering/array.cir | 2 +- clang/test/CIR/Lowering/attribute-lowering.cir | 2 +- clang/test/CIR/Lowering/binop-fp.cir | 2 +- clang/test/CIR/Lowering/binop-overflow.cir | 2 +- clang/test/CIR/Lowering/binop-unsigned-int.cir | 2 +- clang/test/CIR/Lowering/bitint.cir | 2 +- clang/test/CIR/Lowering/bool-to-int.cir | 2 +- clang/test/CIR/Lowering/bool.cir | 2 +- clang/test/CIR/Lowering/branch.cir | 2 +- clang/test/CIR/Lowering/brcond.cir | 2 +- clang/test/CIR/Lowering/bswap.cir | 2 +- clang/test/CIR/Lowering/call-op-call-conv.cir | 2 +- clang/test/CIR/Lowering/call.cir | 2 +- clang/test/CIR/Lowering/cmp3way.cir | 2 +- clang/test/CIR/Lowering/complex.cir | 2 +- clang/test/CIR/Lowering/const-array.cir | 2 +- clang/test/CIR/Lowering/expect.cir | 2 +- clang/test/CIR/Lowering/func-call-conv.cir | 2 +- clang/test/CIR/Lowering/globals.cir | 2 +- clang/test/CIR/Lowering/if.cir | 2 +- clang/test/CIR/Lowering/int-wrap.cir | 2 +- clang/test/CIR/Lowering/intrinsics.cir | 2 +- clang/test/CIR/Lowering/ptrdiff.cir | 2 +- clang/test/CIR/Lowering/region-simplify.cir | 2 +- clang/test/CIR/Lowering/scope.cir | 2 +- clang/test/CIR/Lowering/select.cir | 2 +- clang/test/CIR/Lowering/syncscope.cir | 2 +- clang/test/CIR/Lowering/unary-inc-dec.cir | 2 +- clang/test/CIR/Lowering/unary-not.cir | 2 +- .../CIR/Tools/cir-translate/no-triple-has-data-layout.cir | 5 +++++ .../CIR/Tools/cir-translate/no-triple-no-data-layout.cir | 5 +++++ clang/test/CIR/Tools/cir-translate/warn-default-triple.cir | 5 ++--- clang/tools/cir-translate/cir-translate.cpp | 4 +++- 37 files changed, 48 insertions(+), 37 deletions(-) diff --git a/clang/test/CIR/Lowering/OpenMP/barrier.cir b/clang/test/CIR/Lowering/OpenMP/barrier.cir index c6a58bb02038..145117ab54a0 100644 --- a/clang/test/CIR/Lowering/OpenMP/barrier.cir +++ b/clang/test/CIR/Lowering/OpenMP/barrier.cir @@ -1,5 +1,5 @@ -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s module { diff --git a/clang/test/CIR/Lowering/OpenMP/parallel.cir b/clang/test/CIR/Lowering/OpenMP/parallel.cir index dd1f0c763053..3422eac75ea0 100644 --- a/clang/test/CIR/Lowering/OpenMP/parallel.cir +++ b/clang/test/CIR/Lowering/OpenMP/parallel.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s !s32i = !cir.int module { diff --git a/clang/test/CIR/Lowering/OpenMP/taskwait.cir b/clang/test/CIR/Lowering/OpenMP/taskwait.cir index bb4f4fff4392..83e8119bc479 100644 --- a/clang/test/CIR/Lowering/OpenMP/taskwait.cir +++ b/clang/test/CIR/Lowering/OpenMP/taskwait.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s module { diff --git a/clang/test/CIR/Lowering/OpenMP/taskyield.cir b/clang/test/CIR/Lowering/OpenMP/taskyield.cir index 63a525d9f75c..a701365b798f 100644 --- a/clang/test/CIR/Lowering/OpenMP/taskyield.cir +++ b/clang/test/CIR/Lowering/OpenMP/taskyield.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s module { diff --git a/clang/test/CIR/Lowering/array.cir b/clang/test/CIR/Lowering/array.cir index 09830b64e5ff..30a5aae7bfae 100644 --- a/clang/test/CIR/Lowering/array.cir +++ b/clang/test/CIR/Lowering/array.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM !s32i = !cir.int !ty_S = !cir.struct diff --git a/clang/test/CIR/Lowering/attribute-lowering.cir b/clang/test/CIR/Lowering/attribute-lowering.cir index 3d2e2bd08e47..71ddf2002a25 100644 --- a/clang/test/CIR/Lowering/attribute-lowering.cir +++ b/clang/test/CIR/Lowering/attribute-lowering.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM !s32i = !cir.int !s8i = !cir.int diff --git a/clang/test/CIR/Lowering/binop-fp.cir b/clang/test/CIR/Lowering/binop-fp.cir index e69a69e6b099..a2800a847c85 100644 --- a/clang/test/CIR/Lowering/binop-fp.cir +++ b/clang/test/CIR/Lowering/binop-fp.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM module { cir.func @foo() { diff --git a/clang/test/CIR/Lowering/binop-overflow.cir b/clang/test/CIR/Lowering/binop-overflow.cir index 68af70aa6abb..6a2ef54c1501 100644 --- a/clang/test/CIR/Lowering/binop-overflow.cir +++ b/clang/test/CIR/Lowering/binop-overflow.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM !u32i = !cir.int !s32i = !cir.int diff --git a/clang/test/CIR/Lowering/binop-unsigned-int.cir b/clang/test/CIR/Lowering/binop-unsigned-int.cir index 8834cc103e5a..b783509d06ed 100644 --- a/clang/test/CIR/Lowering/binop-unsigned-int.cir +++ b/clang/test/CIR/Lowering/binop-unsigned-int.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !u32i = !cir.int module { diff --git a/clang/test/CIR/Lowering/bitint.cir b/clang/test/CIR/Lowering/bitint.cir index ea6dcf989aa4..61db545b0d07 100644 --- a/clang/test/CIR/Lowering/bitint.cir +++ b/clang/test/CIR/Lowering/bitint.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s32i = !cir.int diff --git a/clang/test/CIR/Lowering/bool-to-int.cir b/clang/test/CIR/Lowering/bool-to-int.cir index 772e5c5c4e45..97ee3c1daee0 100644 --- a/clang/test/CIR/Lowering/bool-to-int.cir +++ b/clang/test/CIR/Lowering/bool-to-int.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s !s32i = !cir.int #false = #cir.bool : !cir.bool diff --git a/clang/test/CIR/Lowering/bool.cir b/clang/test/CIR/Lowering/bool.cir index 4398c60cc6ed..848b552f897a 100644 --- a/clang/test/CIR/Lowering/bool.cir +++ b/clang/test/CIR/Lowering/bool.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM #false = #cir.bool : !cir.bool #true = #cir.bool : !cir.bool diff --git a/clang/test/CIR/Lowering/branch.cir b/clang/test/CIR/Lowering/branch.cir index ed8eea362039..0daea329f4b8 100644 --- a/clang/test/CIR/Lowering/branch.cir +++ b/clang/test/CIR/Lowering/branch.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s32i = !cir.int cir.func @foo(%arg0: !cir.bool) -> !s32i { diff --git a/clang/test/CIR/Lowering/brcond.cir b/clang/test/CIR/Lowering/brcond.cir index bacddbcdaa0f..19e778cef823 100644 --- a/clang/test/CIR/Lowering/brcond.cir +++ b/clang/test/CIR/Lowering/brcond.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s32i = !cir.int #fn_attr = #cir, nothrow = #cir.nothrow, optnone = #cir.optnone})> diff --git a/clang/test/CIR/Lowering/bswap.cir b/clang/test/CIR/Lowering/bswap.cir index dc3a0744d0be..0f8478ba8936 100644 --- a/clang/test/CIR/Lowering/bswap.cir +++ b/clang/test/CIR/Lowering/bswap.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !u32i = !cir.int diff --git a/clang/test/CIR/Lowering/call-op-call-conv.cir b/clang/test/CIR/Lowering/call-op-call-conv.cir index d4360158575f..92f0028e7bae 100644 --- a/clang/test/CIR/Lowering/call-op-call-conv.cir +++ b/clang/test/CIR/Lowering/call-op-call-conv.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering %s -o %t.ll +// RUN: cir-translate -cir-to-llvmir --disable-cc-lowering %s -o %t.ll // RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM !s32i = !cir.int diff --git a/clang/test/CIR/Lowering/call.cir b/clang/test/CIR/Lowering/call.cir index 1b57a9fd92df..07957727e861 100644 --- a/clang/test/CIR/Lowering/call.cir +++ b/clang/test/CIR/Lowering/call.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM // XFAIL: * !s32i = !cir.int diff --git a/clang/test/CIR/Lowering/cmp3way.cir b/clang/test/CIR/Lowering/cmp3way.cir index 3eda6dd9fcc9..9c18dfce5769 100644 --- a/clang/test/CIR/Lowering/cmp3way.cir +++ b/clang/test/CIR/Lowering/cmp3way.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s8i = !cir.int !s32i = !cir.int diff --git a/clang/test/CIR/Lowering/complex.cir b/clang/test/CIR/Lowering/complex.cir index 40cbf63cc2fb..27180865e377 100644 --- a/clang/test/CIR/Lowering/complex.cir +++ b/clang/test/CIR/Lowering/complex.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o %t.ll %s +// RUN: cir-translate -cir-to-llvmir --disable-cc-lowering -o %t.ll %s // RUN: FileCheck --input-file %t.ll -check-prefix=LLVM %s !s32i = !cir.int diff --git a/clang/test/CIR/Lowering/const-array.cir b/clang/test/CIR/Lowering/const-array.cir index cf16cca3f5b1..84a21665bffd 100644 --- a/clang/test/CIR/Lowering/const-array.cir +++ b/clang/test/CIR/Lowering/const-array.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM !u8i = !cir.int #false = #cir.bool : !cir.bool diff --git a/clang/test/CIR/Lowering/expect.cir b/clang/test/CIR/Lowering/expect.cir index aa300745d890..57f9cf2e35da 100644 --- a/clang/test/CIR/Lowering/expect.cir +++ b/clang/test/CIR/Lowering/expect.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s64i = !cir.int module { diff --git a/clang/test/CIR/Lowering/func-call-conv.cir b/clang/test/CIR/Lowering/func-call-conv.cir index cf73d0bae285..577eb854d47b 100644 --- a/clang/test/CIR/Lowering/func-call-conv.cir +++ b/clang/test/CIR/Lowering/func-call-conv.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o %t.ll +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o %t.ll // RUN: FileCheck %s --input-file=%t.ll --check-prefix=LLVM !s32i = !cir.int diff --git a/clang/test/CIR/Lowering/globals.cir b/clang/test/CIR/Lowering/globals.cir index 58a28490d007..3f99fd102efd 100644 --- a/clang/test/CIR/Lowering/globals.cir +++ b/clang/test/CIR/Lowering/globals.cir @@ -1,6 +1,6 @@ // RUN: cir-opt %s -cir-to-llvm -o %t.mlir // RUN: FileCheck --input-file=%t.mlir %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o %t.ll +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o %t.ll // RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM !void = !cir.void diff --git a/clang/test/CIR/Lowering/if.cir b/clang/test/CIR/Lowering/if.cir index 3a077aa9ef05..44aa412ffd13 100644 --- a/clang/test/CIR/Lowering/if.cir +++ b/clang/test/CIR/Lowering/if.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s32i = !cir.int module { diff --git a/clang/test/CIR/Lowering/int-wrap.cir b/clang/test/CIR/Lowering/int-wrap.cir index b5fb79ba69ed..f885e745004b 100644 --- a/clang/test/CIR/Lowering/int-wrap.cir +++ b/clang/test/CIR/Lowering/int-wrap.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s32i = !cir.int module { diff --git a/clang/test/CIR/Lowering/intrinsics.cir b/clang/test/CIR/Lowering/intrinsics.cir index e5ac27d89bfc..778aeb9f9182 100644 --- a/clang/test/CIR/Lowering/intrinsics.cir +++ b/clang/test/CIR/Lowering/intrinsics.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM module { cir.func @test_unreachable() { diff --git a/clang/test/CIR/Lowering/ptrdiff.cir b/clang/test/CIR/Lowering/ptrdiff.cir index a572367489d9..c0b1a4b3e314 100644 --- a/clang/test/CIR/Lowering/ptrdiff.cir +++ b/clang/test/CIR/Lowering/ptrdiff.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s !s32i = !cir.int !u64i = !cir.int diff --git a/clang/test/CIR/Lowering/region-simplify.cir b/clang/test/CIR/Lowering/region-simplify.cir index 8f1589eea9f9..a76d73d03d8e 100644 --- a/clang/test/CIR/Lowering/region-simplify.cir +++ b/clang/test/CIR/Lowering/region-simplify.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -canonicalize -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-opt %s -canonicalize -o - | cir-translate -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-opt %s -canonicalize -o - | cir-translate -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !u32i = !cir.int diff --git a/clang/test/CIR/Lowering/scope.cir b/clang/test/CIR/Lowering/scope.cir index 092c551a9fd6..850b1ec5e051 100644 --- a/clang/test/CIR/Lowering/scope.cir +++ b/clang/test/CIR/Lowering/scope.cir @@ -1,6 +1,6 @@ // RUN: cir-opt %s -cir-to-llvm -o %t.cir // RUN: FileCheck %s --input-file=%t.cir -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !u32i = !cir.int module { diff --git a/clang/test/CIR/Lowering/select.cir b/clang/test/CIR/Lowering/select.cir index 5067355ca8f6..71ca79a390e8 100644 --- a/clang/test/CIR/Lowering/select.cir +++ b/clang/test/CIR/Lowering/select.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o %t.ll %s +// RUN: cir-translate -cir-to-llvmir --disable-cc-lowering -o %t.ll %s // RUN: FileCheck --input-file=%t.ll -check-prefix=LLVM %s !s32i = !cir.int diff --git a/clang/test/CIR/Lowering/syncscope.cir b/clang/test/CIR/Lowering/syncscope.cir index fe324bba3524..8e1aad8a743c 100644 --- a/clang/test/CIR/Lowering/syncscope.cir +++ b/clang/test/CIR/Lowering/syncscope.cir @@ -1,4 +1,4 @@ -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering -o - | FileCheck %s -check-prefix=LLVM !s32i = !cir.int #fn_attr = #cir, nothrow = #cir.nothrow, optnone = #cir.optnone})> diff --git a/clang/test/CIR/Lowering/unary-inc-dec.cir b/clang/test/CIR/Lowering/unary-inc-dec.cir index 46d2f1115c59..4dac6ac55318 100644 --- a/clang/test/CIR/Lowering/unary-inc-dec.cir +++ b/clang/test/CIR/Lowering/unary-inc-dec.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s32i = !cir.int module { cir.func @foo() { diff --git a/clang/test/CIR/Lowering/unary-not.cir b/clang/test/CIR/Lowering/unary-not.cir index 1ad25d790f35..35cd54f3df78 100644 --- a/clang/test/CIR/Lowering/unary-not.cir +++ b/clang/test/CIR/Lowering/unary-not.cir @@ -1,5 +1,5 @@ // RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM +// RUN: cir-translate %s -cir-to-llvmir --disable-cc-lowering | FileCheck %s -check-prefix=LLVM !s32i = !cir.int module { cir.func @foo() -> !s32i { diff --git a/clang/test/CIR/Tools/cir-translate/no-triple-has-data-layout.cir b/clang/test/CIR/Tools/cir-translate/no-triple-has-data-layout.cir index 5dc2fdb75a7c..f2853941271f 100644 --- a/clang/test/CIR/Tools/cir-translate/no-triple-has-data-layout.cir +++ b/clang/test/CIR/Tools/cir-translate/no-triple-has-data-layout.cir @@ -2,6 +2,8 @@ // RUN: FileCheck %s -input-file %t.x86.ll -check-prefix=X86 // RUN: cir-translate --cir-to-llvmir --target spirv64-unknown-unknown --disable-cc-lowering %s -o %t.spirv64.ll // RUN: FileCheck %s -input-file %t.spirv64.ll -check-prefix=SPIRV64 +// RUN: cir-translate --cir-to-llvmir --disable-cc-lowering %s -o %t.default.ll +// RUN: FileCheck %s -input-file %t.default.ll -check-prefix=DEFAULT module attributes { dlti.dl_spec = #dlti.dl_spec<"dlti.global_memory_space" = 7 : ui64> @@ -16,3 +18,6 @@ module attributes { // SPIRV64-NOT: target datalayout = "G7" // SPIRV64-DAG: target triple = "spirv64-unknown-unknown" + +// DEFAULT-NOT: target datalayout = "G7" +// DEFAULT-DAG: target triple = "x86_64-unknown-linux-gnu" diff --git a/clang/test/CIR/Tools/cir-translate/no-triple-no-data-layout.cir b/clang/test/CIR/Tools/cir-translate/no-triple-no-data-layout.cir index 2e336a797c6a..f18f69dd876d 100644 --- a/clang/test/CIR/Tools/cir-translate/no-triple-no-data-layout.cir +++ b/clang/test/CIR/Tools/cir-translate/no-triple-no-data-layout.cir @@ -2,6 +2,8 @@ // RUN: FileCheck %s -input-file %t.x86.ll -check-prefix=X86 // RUN: cir-translate --cir-to-llvmir --target spirv64-unknown-unknown --disable-cc-lowering %s -o %t.spirv64.ll // RUN: FileCheck %s -input-file %t.spirv64.ll -check-prefix=SPIRV64 +// RUN: cir-translate --cir-to-llvmir --disable-cc-lowering %s -o %t.default.ll +// RUN: FileCheck %s -input-file %t.default.ll -check-prefix=DEFAULT module { cir.func @foo() { @@ -14,3 +16,6 @@ module { // SPIRV64-DAG: target triple = "spirv64-unknown-unknown" // SPIRV64-DAG: target datalayout = "{{.*}}" + +// DEFAULT-DAG: target triple = "x86_64-unknown-linux-gnu" +// DEFAULT-DAG: target datalayout = "{{.*}}" diff --git a/clang/test/CIR/Tools/cir-translate/warn-default-triple.cir b/clang/test/CIR/Tools/cir-translate/warn-default-triple.cir index 9d60324905f7..519e96598d43 100644 --- a/clang/test/CIR/Tools/cir-translate/warn-default-triple.cir +++ b/clang/test/CIR/Tools/cir-translate/warn-default-triple.cir @@ -1,7 +1,6 @@ -// XFAIL: target={{.*windows.*}} -// RUN: cir-translate -verify-diagnostics --cir-to-llvmir --disable-cc-lowering %s 2>&1 +// RUN: cir-translate -verify-diagnostics --cir-to-llvmir --disable-cc-lowering %s -// expected-warning@below {{no target triple provided, assuming}} +// expected-warning@below {{no target triple provided, assuming x86_64-unknown-linux-gnu}} module { cir.func @foo() { cir.return diff --git a/clang/tools/cir-translate/cir-translate.cpp b/clang/tools/cir-translate/cir-translate.cpp index d0e33b45ccf4..bc8fea1f635e 100644 --- a/clang/tools/cir-translate/cir-translate.cpp +++ b/clang/tools/cir-translate/cir-translate.cpp @@ -69,7 +69,9 @@ std::string prepareCIRModuleTriple(mlir::ModuleOp mod) { // Treat "" as the default target machine. if (triple.empty()) { - triple = llvm::sys::getDefaultTargetTriple(); + // Currently ClangIR only supports a couple of targets. Not specifying a + // target triple will default to x86_64-unknown-linux-gnu. + triple = "x86_64-unknown-linux-gnu"; mod.emitWarning() << "no target triple provided, assuming " << triple; } From 1346ee476eb7558cfa83e2a4f42ec2a5458401e2 Mon Sep 17 00:00:00 2001 From: Bruno Cardoso Lopes Date: Thu, 20 Mar 2025 14:38:39 -0700 Subject: [PATCH 11/18] [CIR] Match comment in upstream https://github.com/llvm/llvm-project/pull/132269 --- clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp b/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp index f923ac69dbeb..70ad67ed666c 100644 --- a/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp +++ b/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp @@ -178,8 +178,8 @@ void CIRCanonicalizePass::runOnOperation() { // Collect operations to apply patterns. llvm::SmallVector ops; getOperation()->walk([&](Operation *op) { - // CastOp here is to perform a manual `fold` in - // applyOpPatternsAndFold + // CastOp and UnaryOp are here to perform a manual `fold` in + // applyOpPatternsGreedily. if (isa(op)) ops.push_back(op); From 19dde35471b840b1cf1ba93d5b0fda804571b67d Mon Sep 17 00:00:00 2001 From: Letu Ren Date: Fri, 21 Mar 2025 05:53:49 +0800 Subject: [PATCH 12/18] [CIR] Backport clang commit to unxfail some builtin call (#1501) Ref: https://github.com/llvm/clangir/commit/cd269fee05a0f78fb53b65f701b4e06e9ddab424 Related: https://github.com/llvm/clangir/issues/1497 --- clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 8 +++++--- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 3 ++- clang/test/CIR/CodeGen/builtins-memory.c | 1 - clang/test/CIR/CodeGen/builtins.cpp | 1 - clang/test/CIR/CodeGen/libcall.cpp | 1 - clang/test/CIR/Lowering/builtin-binary-fp2fp.c | 1 - 6 files changed, 7 insertions(+), 8 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index 075c951f1086..a3de06ebef9a 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -2772,8 +2772,10 @@ cir::FuncOp CIRGenModule::getBuiltinLibFunction(const FunctionDecl *FD, unsigned BuiltinID) { assert(astContext.BuiltinInfo.isLibFunction(BuiltinID)); - // Get the name, skip over the __builtin_ prefix (if necessary). - StringRef Name; + // Get the name, skip over the __builtin_ prefix (if necessary). We may have + // to build this up so provide a small stack buffer to handle the vast + // majority of names. + llvm::SmallString<64> Name; GlobalDecl D(FD); // TODO: This list should be expanded or refactored after all GCC-compatible @@ -2832,7 +2834,7 @@ cir::FuncOp CIRGenModule::getBuiltinLibFunction(const FunctionDecl *FD, AIXLongDouble64Builtins.end()) Name = AIXLongDouble64Builtins[BuiltinID]; else - Name = StringRef(astContext.BuiltinInfo.getName(BuiltinID).data(), 10); + Name = astContext.BuiltinInfo.getName(BuiltinID).substr(10); } auto Ty = convertType(FD->getType()); diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index d83a00626967..90c632ec7066 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -3695,7 +3695,8 @@ struct FunctionIsDirectlyRecursive unsigned builtinId = func->getBuiltinID(); if (!builtinId || !builtinCtx.isLibFunction(builtinId)) return false; - StringRef builtinName = builtinCtx.getName(builtinId); + std::string builtinNameStr = builtinCtx.getName(builtinId); + StringRef builtinName = builtinNameStr; return builtinName.starts_with("__builtin_") && name == builtinName.slice(strlen("__builtin_"), StringRef::npos); } diff --git a/clang/test/CIR/CodeGen/builtins-memory.c b/clang/test/CIR/CodeGen/builtins-memory.c index a6401b92eb12..9c7a74301aaa 100644 --- a/clang/test/CIR/CodeGen/builtins-memory.c +++ b/clang/test/CIR/CodeGen/builtins-memory.c @@ -3,7 +3,6 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o - \ // RUN: | opt -S -passes=instcombine,mem2reg,simplifycfg -o %t.ll // RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s -// XFAIL: * typedef __SIZE_TYPE__ size_t; void test_memcpy_chk(void *dest, const void *src, size_t n) { diff --git a/clang/test/CIR/CodeGen/builtins.cpp b/clang/test/CIR/CodeGen/builtins.cpp index a0e27c49c3d7..504ec13da6ee 100644 --- a/clang/test/CIR/CodeGen/builtins.cpp +++ b/clang/test/CIR/CodeGen/builtins.cpp @@ -4,7 +4,6 @@ // RUN: -emit-llvm -fno-clangir-call-conv-lowering -o - %s \ // RUN: | opt -S -passes=instcombine,mem2reg,simplifycfg -o %t.ll // RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s -// XFAIL: * // This test file is a collection of test cases for all target-independent // builtins that are related to memory operations. diff --git a/clang/test/CIR/CodeGen/libcall.cpp b/clang/test/CIR/CodeGen/libcall.cpp index 990240e75746..192b0ff13294 100644 --- a/clang/test/CIR/CodeGen/libcall.cpp +++ b/clang/test/CIR/CodeGen/libcall.cpp @@ -1,6 +1,5 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -mconstructor-aliases -emit-cir %s -o %t.cir // RUN: FileCheck --input-file=%t.cir %s -// XFAIL: * typedef __builtin_va_list va_list; diff --git a/clang/test/CIR/Lowering/builtin-binary-fp2fp.c b/clang/test/CIR/Lowering/builtin-binary-fp2fp.c index d44444769889..2877aa5cef30 100644 --- a/clang/test/CIR/Lowering/builtin-binary-fp2fp.c +++ b/clang/test/CIR/Lowering/builtin-binary-fp2fp.c @@ -2,7 +2,6 @@ // RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -ffast-math -fclangir -emit-llvm %s -o %t.ll // RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM-FASTMATH -// XFAIL: * // copysign From 765ab6e274bf6c201b07f0584b5c09e2fc28d655 Mon Sep 17 00:00:00 2001 From: Iris Shi <0.0@owo.li> Date: Wed, 19 Mar 2025 18:41:36 +0800 Subject: [PATCH 13/18] [CIR] Refactor `StructType` with TableGen --- clang/include/clang/CIR/Dialect/IR/CIRTypes.h | 162 +----------------- .../include/clang/CIR/Dialect/IR/CIRTypes.td | 154 ++++++++++++++++- clang/lib/CIR/Dialect/IR/CIRTypes.cpp | 58 +------ 3 files changed, 158 insertions(+), 216 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.h b/clang/include/clang/CIR/Dialect/IR/CIRTypes.h index 8814887b679b..fa5a9de34281 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.h +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.h @@ -16,173 +16,15 @@ #include "mlir/IR/BuiltinAttributes.h" #include "mlir/IR/Types.h" #include "mlir/Interfaces/DataLayoutInterfaces.h" -#include "clang/CIR/Interfaces/CIRFPTypeInterface.h" - -#include "clang/CIR/Interfaces/ASTAttrInterfaces.h" - #include "clang/CIR/Dialect/IR/CIROpsEnums.h" - -//===----------------------------------------------------------------------===// -// CIR StructType -// -// The base type for all RecordDecls. -//===----------------------------------------------------------------------===// +#include "clang/CIR/Interfaces/ASTAttrInterfaces.h" +#include "clang/CIR/Interfaces/CIRFPTypeInterface.h" namespace cir { - namespace detail { struct StructTypeStorage; } // namespace detail -/// Each unique clang::RecordDecl is mapped to a `cir.struct` and any object in -/// C/C++ that has a struct type will have a `cir.struct` in CIR. -/// -/// There are three possible formats for this type: -/// -/// - Identified and complete structs: unique name and a known body. -/// - Identified and incomplete structs: unique name and unknown body. -/// - Anonymous structs: no name and a known body. -/// -/// Identified structs are uniqued by their name, and anonymous structs are -/// uniqued by their body. This means that two anonymous structs with the same -/// body will be the same type, and two identified structs with the same name -/// will be the same type. Attempting to build a struct with an existing name, -/// but a different body will result in an error. -/// -/// A few examples: -/// -/// ```mlir -/// !complete = !cir.struct}> -/// !incomplete = !cir.struct -/// !anonymous = !cir.struct}> -/// ``` -/// -/// Incomplete structs are mutable, meaning they can be later completed with a -/// body automatically updating in place every type in the code that uses the -/// incomplete struct. Mutability allows for recursive types to be represented, -/// meaning the struct can have members that refer to itself. This is useful for -/// representing recursive records and is implemented through a special syntax. -/// In the example below, the `Node` struct has a member that is a pointer to a -/// `Node` struct: -/// -/// ```mlir -/// !struct = !cir.struct>}> -/// ``` -class StructType - : public mlir::Type::TypeBase< - StructType, mlir::Type, detail::StructTypeStorage, - mlir::DataLayoutTypeInterface::Trait, mlir::TypeTrait::IsMutable> { - // FIXME(cir): migrate this type to Tablegen once mutable types are supported. -public: - using Base::Base; - using Base::getChecked; - using Base::verifyInvariants; - - static constexpr llvm::StringLiteral name = "cir.struct"; - - enum RecordKind : uint32_t { Class, Union, Struct }; - - /// Create an identified and complete struct type. - static StructType get(mlir::MLIRContext *context, - llvm::ArrayRef members, - mlir::StringAttr name, bool packed, bool padded, - RecordKind kind, ASTRecordDeclInterface ast = {}); - static StructType - getChecked(llvm::function_ref emitError, - mlir::MLIRContext *context, llvm::ArrayRef members, - mlir::StringAttr name, bool packed, bool padded, RecordKind kind, - ASTRecordDeclInterface ast = {}); - - /// Create an identified and incomplete struct type. - static StructType get(mlir::MLIRContext *context, mlir::StringAttr name, - RecordKind kind); - static StructType - getChecked(llvm::function_ref emitError, - mlir::MLIRContext *context, mlir::StringAttr name, - RecordKind kind); - - /// Create an anonymous struct type (always complete). - static StructType get(mlir::MLIRContext *context, - llvm::ArrayRef members, bool packed, - bool padded, RecordKind kind, - ASTRecordDeclInterface ast = {}); - static StructType - getChecked(llvm::function_ref emitError, - mlir::MLIRContext *context, llvm::ArrayRef members, - bool packed, bool padded, RecordKind kind, - ASTRecordDeclInterface ast = {}); - - /// Validate the struct about to be constructed. - static llvm::LogicalResult - verifyInvariants(llvm::function_ref emitError, - llvm::ArrayRef members, mlir::StringAttr name, - bool incomplete, bool packed, bool padded, - StructType::RecordKind kind, ASTRecordDeclInterface ast); - - // Parse/print methods. - static constexpr llvm::StringLiteral getMnemonic() { return {"struct"}; } - static mlir::Type parse(mlir::AsmParser &odsParser); - void print(mlir::AsmPrinter &odsPrinter) const; - - // Accessors - ASTRecordDeclInterface getAst() const; - llvm::ArrayRef getMembers() const; - mlir::StringAttr getName() const; - StructType::RecordKind getKind() const; - bool getIncomplete() const; - bool getPacked() const; - bool getPadded() const; - void dropAst(); - - // Predicates - bool isClass() const { return getKind() == RecordKind::Class; }; - bool isStruct() const { return getKind() == RecordKind::Struct; }; - bool isUnion() const { return getKind() == RecordKind::Union; }; - bool isComplete() const { return !isIncomplete(); }; - bool isIncomplete() const; - - // Utilities - mlir::Type getLargestMember(const mlir::DataLayout &dataLayout) const; - size_t getNumElements() const { return getMembers().size(); }; - std::string getKindAsStr() { - switch (getKind()) { - case RecordKind::Class: - return "class"; - case RecordKind::Union: - return "union"; - case RecordKind::Struct: - return "struct"; - } - llvm_unreachable("Invalid value for StructType::getKind()"); - } - std::string getPrefixedName() { - return getKindAsStr() + "." + getName().getValue().str(); - } - - /// Complete the struct type by mutating its members and attributes. - void complete(llvm::ArrayRef members, bool packed, bool isPadded, - ASTRecordDeclInterface ast = {}); - - /// DataLayoutTypeInterface methods. - llvm::TypeSize getTypeSizeInBits(const mlir::DataLayout &dataLayout, - mlir::DataLayoutEntryListRef params) const; - uint64_t getABIAlignment(const mlir::DataLayout &dataLayout, - mlir::DataLayoutEntryListRef params) const; - uint64_t getElementOffset(const mlir::DataLayout &dataLayout, - unsigned idx) const; - - bool isLayoutIdentical(const StructType &other); - - // Utilities for lazily computing and cacheing data layout info. -private: - // FIXME: currently opaque because there's a cycle if CIRTypes.types include - // from CIRAttrs.h. The implementation operates in terms of StructLayoutAttr - // instead. - mutable mlir::Attribute layoutInfo; - void computeSizeAndAlignment(const mlir::DataLayout &dataLayout) const; -}; - bool isAnyFloatingPointType(mlir::Type t); bool isScalarType(mlir::Type t); bool isFPOrFPVectorTy(mlir::Type); diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index 80f6040f812b..6a11a7576d61 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -603,11 +603,159 @@ def FuncPtr : Type< } //===----------------------------------------------------------------------===// -// StructType (defined in cpp files) +// StructType +// +// The base type for all RecordDecls. //===----------------------------------------------------------------------===// -def CIR_StructType : Type($_self)">, - "CIR struct type">; +def CIR_StructType : CIR_Type<"Struct", "struct", + [ + DeclareTypeInterfaceMethods, + MutableType, + ]> { + let summary = "CIR struct type"; + let description = [{ + Each unique clang::RecordDecl is mapped to a `cir.struct` and any object in + C/C++ that has a struct type will have a `cir.struct` in CIR. + + There are three possible formats for this type: + + - Identified and complete structs: unique name and a known body. + - Identified and incomplete structs: unique name and unknown body. + - Anonymous structs: no name and a known body. + + Identified structs are uniqued by their name, and anonymous structs are + uniqued by their body. This means that two anonymous structs with the same + body will be the same type, and two identified structs with the same name + will be the same type. Attempting to build a struct with an existing name, + but a different body will result in an error. + + A few examples: + + ```mlir + !complete = !cir.struct}> + !incomplete = !cir.struct + !anonymous = !cir.struct}> + ``` + + Incomplete structs are mutable, meaning they can be later completed with a + body automatically updating in place every type in the code that uses the + incomplete struct. Mutability allows for recursive types to be represented, + meaning the struct can have members that refer to itself. This is useful for + representing recursive records and is implemented through a special syntax. + In the example below, the `Node` struct has a member that is a pointer to a + `Node` struct: + + ```mlir + !struct = !cir.struct>}> + ``` + }]; + + let parameters = (ins + OptionalArrayRefParameter<"mlir::Type">:$members, + OptionalParameter<"mlir::StringAttr">:$name, + "bool":$incomplete, + "bool":$packed, + "bool":$padded, + "StructType::RecordKind":$kind, + OptionalParameter<"ASTRecordDeclInterface">:$ast + ); + + // StorageClass is defined in C++ for mutability. + let storageClass = "StructTypeStorage"; + let genStorageClass = 0; + + let skipDefaultBuilders = 1; + let genVerifyDecl = 1; + + let builders = [ + // Create an identified and complete struct type. + TypeBuilder<(ins + "llvm::ArrayRef":$members, + "mlir::StringAttr":$name, + "bool":$packed, + "bool":$padded, + "RecordKind":$kind, + CArg<"ASTRecordDeclInterface", "{}">:$ast + ), [{ + return $_get($_ctxt, members, name, /*incomplete=*/false, packed, padded, + kind, ast); + }]>, + + // Create an identified and incomplete struct type. + TypeBuilder<(ins + "mlir::StringAttr":$name, + "RecordKind":$kind + ), [{ + return $_get($_ctxt, /*members=*/llvm::ArrayRef{}, name, + /*incomplete=*/true, /*packed=*/false, /*padded=*/false, + kind, + /*ast=*/ASTRecordDeclInterface{}); + }]>, + + // Create an anonymous struct type (always complete). + TypeBuilder<(ins + "llvm::ArrayRef":$members, + "bool":$packed, + "bool":$padded, + "RecordKind":$kind, + CArg<"ASTRecordDeclInterface", "{}">:$ast + ), [{ + return $_get($_ctxt, members, mlir::StringAttr{}, /*incomplete=*/false, packed, + padded, kind, ast); + }]>]; + + let extraClassDeclaration = [{ + using Base::verifyInvariants; + + enum RecordKind : uint32_t { Class, Union, Struct }; + + bool isClass() const { return getKind() == RecordKind::Class; }; + bool isStruct() const { return getKind() == RecordKind::Struct; }; + bool isUnion() const { return getKind() == RecordKind::Union; }; + bool isComplete() const { return !isIncomplete(); }; + bool isIncomplete() const; + + void dropAst(); + + mlir::Type getLargestMember(const mlir::DataLayout &dataLayout) const; + size_t getNumElements() const { return getMembers().size(); }; + std::string getKindAsStr() { + switch (getKind()) { + case RecordKind::Class: + return "class"; + case RecordKind::Union: + return "union"; + case RecordKind::Struct: + return "struct"; + } + llvm_unreachable("Invalid value for StructType::getKind()"); + } + std::string getPrefixedName() { + return getKindAsStr() + "." + getName().getValue().str(); + } + + void complete(llvm::ArrayRef members, bool packed, bool isPadded, + ASTRecordDeclInterface ast = {}); + + uint64_t getElementOffset(const mlir::DataLayout &dataLayout, + unsigned idx) const; + + bool isLayoutIdentical(const StructType &other); + + // Utilities for lazily computing and cacheing data layout info. + // FIXME: currently opaque because there's a cycle if CIRTypes.types include + // from CIRAttrs.h. The implementation operates in terms of StructLayoutAttr + // instead. + private: + mutable mlir::Attribute layoutInfo; + void computeSizeAndAlignment(const mlir::DataLayout &dataLayout) const; + public: + }]; + + let hasCustomAssemblyFormat = 1; +} //===----------------------------------------------------------------------===// // Global type constraints diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp index dd35b87f471d..8c739721f0d3 100644 --- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp @@ -264,11 +264,11 @@ void StructType::print(mlir::AsmPrinter &printer) const { printer << '>'; } -mlir::LogicalResult StructType::verifyInvariants( - llvm::function_ref emitError, - llvm::ArrayRef members, mlir::StringAttr name, bool incomplete, - bool packed, bool padded, cir::StructType::RecordKind kind, - ASTRecordDeclInterface ast) { +mlir::LogicalResult +StructType::verify(function_ref emitError, + llvm::ArrayRef members, mlir::StringAttr name, + bool incomplete, bool packed, bool padded, + StructType::RecordKind kind, ASTRecordDeclInterface ast) { if (name && name.getValue().empty()) { emitError() << "identified structs cannot have an empty name"; return mlir::failure(); @@ -277,51 +277,6 @@ mlir::LogicalResult StructType::verifyInvariants( } void StructType::dropAst() { getImpl()->ast = nullptr; } -StructType StructType::get(::mlir::MLIRContext *context, ArrayRef members, - StringAttr name, bool packed, bool padded, - RecordKind kind, ASTRecordDeclInterface ast) { - return Base::get(context, members, name, /*incomplete=*/false, packed, padded, - kind, ast); -} - -StructType StructType::getChecked( - ::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, - ::mlir::MLIRContext *context, ArrayRef members, StringAttr name, - bool packed, bool padded, RecordKind kind, ASTRecordDeclInterface ast) { - return Base::getChecked(emitError, context, members, name, - /*incomplete=*/false, packed, padded, kind, ast); -} - -StructType StructType::get(::mlir::MLIRContext *context, StringAttr name, - RecordKind kind) { - return Base::get(context, /*members=*/ArrayRef{}, name, - /*incomplete=*/true, /*packed=*/false, /*padded=*/false, - kind, - /*ast=*/ASTRecordDeclInterface{}); -} - -StructType StructType::getChecked( - ::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, - ::mlir::MLIRContext *context, StringAttr name, RecordKind kind) { - return Base::getChecked(emitError, context, ArrayRef{}, name, - /*incomplete=*/true, /*packed=*/false, - /*padded=*/false, kind, ASTRecordDeclInterface{}); -} - -StructType StructType::get(::mlir::MLIRContext *context, ArrayRef members, - bool packed, bool padded, RecordKind kind, - ASTRecordDeclInterface ast) { - return Base::get(context, members, StringAttr{}, /*incomplete=*/false, packed, - padded, kind, ast); -} - -StructType StructType::getChecked( - ::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, - ::mlir::MLIRContext *context, ArrayRef members, bool packed, - bool padded, RecordKind kind, ASTRecordDeclInterface ast) { - return Base::getChecked(emitError, context, members, StringAttr{}, - /*incomplete=*/false, packed, padded, kind, ast); -} ::llvm::ArrayRef StructType::getMembers() const { return getImpl()->members; @@ -1009,7 +964,4 @@ void CIRDialect::registerTypes() { #define GET_TYPEDEF_LIST #include "clang/CIR/Dialect/IR/CIROpsTypes.cpp.inc" >(); - - // Register raw C++ types. - addTypes(); } From f6cb7b2e297fb007f3f38b58789eee19d330a8ee Mon Sep 17 00:00:00 2001 From: Iris Shi <0.0@owo.li> Date: Thu, 20 Mar 2025 12:23:32 +0800 Subject: [PATCH 14/18] fix `*MemberOp` --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 6 +++--- clang/include/clang/CIR/Dialect/IR/CIRTypes.td | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 830da08a27fe..006663604dcc 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -2920,7 +2920,7 @@ def ExtractMemberOp : CIR_Op<"extract_member", [Pure]> { ``` }]; - let arguments = (ins CIR_StructType:$record, IndexAttr:$index_attr); + let arguments = (ins Struct:$record, IndexAttr:$index_attr); let results = (outs CIR_AnyType:$result); let assemblyFormat = [{ @@ -2984,9 +2984,9 @@ def InsertMemberOp : CIR_Op<"insert_member", ``` }]; - let arguments = (ins CIR_StructType:$record, IndexAttr:$index_attr, + let arguments = (ins Struct:$record, IndexAttr:$index_attr, CIR_AnyType:$value); - let results = (outs CIR_StructType:$result); + let results = (outs Struct:$result); let builders = [ OpBuilder<(ins "mlir::Value":$record, "uint64_t":$index, diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index 6a11a7576d61..4ba4002396fa 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -757,6 +757,8 @@ def CIR_StructType : CIR_Type<"Struct", "struct", let hasCustomAssemblyFormat = 1; } +def Struct : Type($_self)">, "CIR struct type">; + //===----------------------------------------------------------------------===// // Global type constraints //===----------------------------------------------------------------------===// From fc3b818aead6843bc11a6d85cb67c5e90260096f Mon Sep 17 00:00:00 2001 From: Iris Shi <0.0@owo.li> Date: Fri, 21 Mar 2025 11:54:06 +0800 Subject: [PATCH 15/18] namings --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 6 +++--- clang/include/clang/CIR/Dialect/IR/CIRTypes.td | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 006663604dcc..92474b1670e3 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -2920,7 +2920,7 @@ def ExtractMemberOp : CIR_Op<"extract_member", [Pure]> { ``` }]; - let arguments = (ins Struct:$record, IndexAttr:$index_attr); + let arguments = (ins CIRStructType:$record, IndexAttr:$index_attr); let results = (outs CIR_AnyType:$result); let assemblyFormat = [{ @@ -2984,9 +2984,9 @@ def InsertMemberOp : CIR_Op<"insert_member", ``` }]; - let arguments = (ins Struct:$record, IndexAttr:$index_attr, + let arguments = (ins CIRStructType:$record, IndexAttr:$index_attr, CIR_AnyType:$value); - let results = (outs Struct:$result); + let results = (outs CIRStructType:$result); let builders = [ OpBuilder<(ins "mlir::Value":$record, "uint64_t":$index, diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index 4ba4002396fa..0e7da2618e31 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -757,7 +757,9 @@ def CIR_StructType : CIR_Type<"Struct", "struct", let hasCustomAssemblyFormat = 1; } -def Struct : Type($_self)">, "CIR struct type">; +// This type is used because of some limitations of type constraints. +// See https://github.com/llvm/clangir/pull/1504#issuecomment-2738968751 for more details. +def CIRStructType : Type($_self)">, "CIR struct type">; //===----------------------------------------------------------------------===// // Global type constraints From 67b3d664565d7c602a333cbd595de2736f37a11c Mon Sep 17 00:00:00 2001 From: Iris <0.0@owo.li> Date: Tue, 25 Mar 2025 08:44:11 +0800 Subject: [PATCH 16/18] Update clang/include/clang/CIR/Dialect/IR/CIRTypes.td --- clang/include/clang/CIR/Dialect/IR/CIRTypes.td | 1 - 1 file changed, 1 deletion(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index 0e7da2618e31..860831fc0ae2 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -757,7 +757,6 @@ def CIR_StructType : CIR_Type<"Struct", "struct", let hasCustomAssemblyFormat = 1; } -// This type is used because of some limitations of type constraints. // See https://github.com/llvm/clangir/pull/1504#issuecomment-2738968751 for more details. def CIRStructType : Type($_self)">, "CIR struct type">; From 0d1cf57e126892359314252ab62a498b16cfe006 Mon Sep 17 00:00:00 2001 From: Iris <0.0@owo.li> Date: Tue, 25 Mar 2025 08:44:21 +0800 Subject: [PATCH 17/18] Update clang/include/clang/CIR/Dialect/IR/CIRTypes.td --- clang/include/clang/CIR/Dialect/IR/CIRTypes.td | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index 860831fc0ae2..2a2ab3ca5608 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -757,7 +757,7 @@ def CIR_StructType : CIR_Type<"Struct", "struct", let hasCustomAssemblyFormat = 1; } -// See https://github.com/llvm/clangir/pull/1504#issuecomment-2738968751 for more details. +// Note CIRStructType is used instead of CIR_StructType because of tablegen conflicts def CIRStructType : Type($_self)">, "CIR struct type">; //===----------------------------------------------------------------------===// From 0a7facc379c7d4a833487e906160a3721a18e1bb Mon Sep 17 00:00:00 2001 From: Iris Shi <0.0@owo.li> Date: Tue, 25 Mar 2025 11:22:43 +0800 Subject: [PATCH 18/18] 80 chars limit --- clang/include/clang/CIR/Dialect/IR/CIRTypes.td | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index 2a2ab3ca5608..0294a78de185 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -689,8 +689,8 @@ def CIR_StructType : CIR_Type<"Struct", "struct", "RecordKind":$kind ), [{ return $_get($_ctxt, /*members=*/llvm::ArrayRef{}, name, - /*incomplete=*/true, /*packed=*/false, /*padded=*/false, - kind, + /*incomplete=*/true, /*packed=*/false, + /*padded=*/false, kind, /*ast=*/ASTRecordDeclInterface{}); }]>, @@ -702,8 +702,8 @@ def CIR_StructType : CIR_Type<"Struct", "struct", "RecordKind":$kind, CArg<"ASTRecordDeclInterface", "{}">:$ast ), [{ - return $_get($_ctxt, members, mlir::StringAttr{}, /*incomplete=*/false, packed, - padded, kind, ast); + return $_get($_ctxt, members, mlir::StringAttr{}, /*incomplete=*/false, + packed, padded, kind, ast); }]>]; let extraClassDeclaration = [{ @@ -736,8 +736,8 @@ def CIR_StructType : CIR_Type<"Struct", "struct", return getKindAsStr() + "." + getName().getValue().str(); } - void complete(llvm::ArrayRef members, bool packed, bool isPadded, - ASTRecordDeclInterface ast = {}); + void complete(llvm::ArrayRef members, bool packed, + bool isPadded, ASTRecordDeclInterface ast = {}); uint64_t getElementOffset(const mlir::DataLayout &dataLayout, unsigned idx) const; @@ -757,8 +757,10 @@ def CIR_StructType : CIR_Type<"Struct", "struct", let hasCustomAssemblyFormat = 1; } -// Note CIRStructType is used instead of CIR_StructType because of tablegen conflicts -def CIRStructType : Type($_self)">, "CIR struct type">; +// Note CIRStructType is used instead of CIR_StructType +// because of tablegen conflicts. +def CIRStructType : Type< + CPred<"::mlir::isa<::cir::StructType>($_self)">, "CIR struct type">; //===----------------------------------------------------------------------===// // Global type constraints