https://github.com/Siya-05 updated https://github.com/llvm/llvm-project/pull/237558
>From 0eadda9e200be8bde9018f4693543387acae167d Mon Sep 17 00:00:00 2001 From: Sivapriya <[email protected]> Date: Sat, 31 Oct 2026 06:42:58 +0000 Subject: [PATCH] [CIR] Verify ABI result extension attribute signedness --- clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 43 +++++++++++- clang/test/CIR/IR/invalid-call.cir | 42 ++++++++++++ .../CIR/IR/invalid-result-extension-attrs.cir | 33 +++++++++ .../extend-return-existing-attrs.cir | 68 +++++++++++++++++++ 4 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 clang/test/CIR/IR/invalid-result-extension-attrs.cir create mode 100644 clang/test/CIR/Transforms/abi-lowering/extend-return-existing-attrs.cir diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index 2a496863cd971f..bcff18172e7235 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -1799,6 +1799,37 @@ void cir::CallOp::print(mlir::OpAsmPrinter &p) { getArgAttrsAttr(), getResAttrsAttr()); } +static LogicalResult verifyResultExtensionAttrs(Operation *op, Type resultType, + ArrayAttr resAttrs) { + if (!resAttrs || resAttrs.empty()) + return success(); + + auto attrs = cast<DictionaryAttr>(resAttrs[0]); + bool hasSignExt = static_cast<bool>(attrs.get("llvm.signext")); + bool hasZeroExt = static_cast<bool>(attrs.get("llvm.zeroext")); + + if (!hasSignExt && !hasZeroExt) + return success(); + + if (hasSignExt && hasZeroExt) + return op->emitOpError( + "result cannot have both llvm.signext and llvm.zeroext"); + + auto intType = dyn_cast<cir::IntType>(resultType); + if (!intType) + return success(); + + if (hasSignExt && intType.isUnsigned()) + return op->emitOpError( + "llvm.signext is incompatible with an unsigned result type"); + + if (hasZeroExt && intType.isSigned()) + return op->emitOpError( + "llvm.zeroext is incompatible with a signed result type"); + + return success(); +} + static LogicalResult verifyCallCommInSymbolUses(mlir::Operation *op, SymbolTableCollection &symbolTable) { @@ -1866,7 +1897,12 @@ verifyCallCommInSymbolUses(mlir::Operation *op, << op->getResult(0).getType(); } - return mlir::success(); + if (op->getNumResults() == 1) + return verifyResultExtensionAttrs( + op, op->getResult(0).getType(), + op->getAttrOfType<ArrayAttr>(CIRDialect::getResAttrsAttrName())); + + return success(); } LogicalResult @@ -3549,6 +3585,11 @@ void cir::FuncOp::print(OpAsmPrinter &p) { mlir::LogicalResult cir::FuncOp::verify() { + if (failed(verifyResultExtensionAttrs(getOperation(), + getFunctionType().getReturnType(), + getResAttrsAttr()))) + return failure(); + if (!isDeclaration() && getCoroutineKind() == cir::CoroutineKind::Coroutine) { int coroutineOpCount = 0; this->walk([&](Operation *op) { diff --git a/clang/test/CIR/IR/invalid-call.cir b/clang/test/CIR/IR/invalid-call.cir index 1c64e2f5a4717c..39a3b62ae0489d 100644 --- a/clang/test/CIR/IR/invalid-call.cir +++ b/clang/test/CIR/IR/invalid-call.cir @@ -284,3 +284,45 @@ cir.func @f32(%arg0: !cir.ptr<!cir.func<(!s32i, ...)>>, %3 = cir.call @f31(%arg3, %arg3) : (!u32i, !u32i) -> !s32i cir.return } + +// ----- +// Result extension signedness regression tests + +!s8i = !cir.int<s, 8> +module { + cir.func private @signed_result() -> !s8i + cir.func @bad_direct_call() { + // expected-error@+1 {{llvm.zeroext is incompatible with a signed result type}} + %0 = cir.call @signed_result() : () -> (!s8i {llvm.zeroext}) + cir.return + } +} + +// ----- + +!u8i = !cir.int<u, 8> +module { + cir.func @bad_indirect_call(%fn: !cir.ptr<!cir.func<() -> !u8i>>) { + // expected-error@+1 {{llvm.signext is incompatible with an unsigned result type}} + %0 = cir.call %fn() : (!cir.ptr<!cir.func<() -> !u8i>>) -> (!u8i {llvm.signext}) + cir.return + } +} + +// ----- + +!s8i = !cir.int<s, 8> +module { + cir.func private @signed_result() -> !s8i + cir.func @bad_try_call() { + // expected-error@+1 {{llvm.zeroext is incompatible with a signed result type}} + %0 = cir.try_call @signed_result() ^normal, ^unwind + : () -> (!s8i {llvm.zeroext}) + ^normal: + cir.br ^end + ^unwind: + cir.br ^end + ^end: + cir.return + } +} diff --git a/clang/test/CIR/IR/invalid-result-extension-attrs.cir b/clang/test/CIR/IR/invalid-result-extension-attrs.cir new file mode 100644 index 00000000000000..645a075be4cdba --- /dev/null +++ b/clang/test/CIR/IR/invalid-result-extension-attrs.cir @@ -0,0 +1,33 @@ +// RUN: cir-opt %s -verify-diagnostics -split-input-file + +!s8i = !cir.int<s, 8> +module { + // expected-error@+1 {{llvm.zeroext is incompatible with a signed result type}} + cir.func private @bad_signed() -> (!s8i {llvm.zeroext}) +} + +// ----- + +!u8i = !cir.int<u, 8> +module { + // expected-error@+1 {{llvm.signext is incompatible with an unsigned result type}} + cir.func private @bad_unsigned() -> (!u8i {llvm.signext}) +} + +// ----- + +!s8i = !cir.int<s, 8> +module { + // expected-error@+1 {{result cannot have both llvm.signext and llvm.zeroext}} + cir.func private @both_extensions() -> (!s8i {llvm.signext, llvm.zeroext}) +} + +// ----- + +!s8i = !cir.int<s, 8> +!u8i = !cir.int<u, 8> +module { + cir.func private @valid_signed() -> (!s8i {llvm.signext}) + cir.func private @valid_unsigned() -> (!u8i {llvm.zeroext}) + cir.func private @valid_bool() -> (!cir.bool {llvm.zeroext}) +} diff --git a/clang/test/CIR/Transforms/abi-lowering/extend-return-existing-attrs.cir b/clang/test/CIR/Transforms/abi-lowering/extend-return-existing-attrs.cir new file mode 100644 index 00000000000000..34743b1039296b --- /dev/null +++ b/clang/test/CIR/Transforms/abi-lowering/extend-return-existing-attrs.cir @@ -0,0 +1,68 @@ + +// RUN: cir-opt %s -cir-call-conv-lowering="classification-attr=test_classify" \ +// RUN: | FileCheck %s + +!s8i = !cir.int<s, 8> +!u8i = !cir.int<u, 8> + +#extend_signed_return = { + return = { kind = "extend", + coerced_type = !cir.int<s, 32>, + sign_extend = true }, + args = [ ] +} + +#extend_unsigned_return = { + return = { kind = "extend", + coerced_type = !cir.int<u, 32>, + sign_extend = false }, + args = [ ] +} + +module attributes { + dlti.dl_spec = #dlti.dl_spec< + #dlti.dl_entry<i8, dense<8>: vector<2xi64>>, + #dlti.dl_entry<i32, dense<32>: vector<2xi64>>> +} { + // An existing signext must be preserved during ABI lowering. + cir.func @returns_s8() -> (!s8i {llvm.signext}) + attributes { test_classify = #extend_signed_return } { + %0 = cir.const #cir.int<7> : !s8i + cir.return %0 : !s8i + } + + // CHECK: cir.func{{.*}} @returns_s8() -> (!s8i {llvm.signext}) + // CHECK: cir.return %{{.*}} : !s8i + + // Check both the caller's result and the call result. + cir.func @caller_s8() -> (!s8i {llvm.signext}) + attributes { test_classify = #extend_signed_return } { + %0 = cir.call @returns_s8() : () -> (!s8i {llvm.signext}) + cir.return %0 : !s8i + } + + // CHECK: cir.func{{.*}} @caller_s8() -> (!s8i {llvm.signext}) + // CHECK: %[[SR:.*]] = cir.call @returns_s8() : () -> (!s8i {llvm.signext}) + // CHECK: cir.return %[[SR]] : !s8i + + // An existing zeroext must be preserved during ABI lowering. + cir.func @returns_u8() -> (!u8i {llvm.zeroext}) + attributes { test_classify = #extend_unsigned_return } { + %0 = cir.const #cir.int<7> : !u8i + cir.return %0 : !u8i + } + + // CHECK: cir.func{{.*}} @returns_u8() -> (!u8i {llvm.zeroext}) + // CHECK: cir.return %{{.*}} : !u8i + + // Check both the caller's result and the call result. + cir.func @caller_u8() -> (!u8i {llvm.zeroext}) + attributes { test_classify = #extend_unsigned_return } { + %0 = cir.call @returns_u8() : () -> (!u8i {llvm.zeroext}) + cir.return %0 : !u8i + } + + // CHECK: cir.func{{.*}} @caller_u8() -> (!u8i {llvm.zeroext}) + // CHECK: %[[UR:.*]] = cir.call @returns_u8() : () -> (!u8i {llvm.zeroext}) + // CHECK: cir.return %[[UR]] : !u8i +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
