[clang] 59de680 - [CIR] Verify record_align is a non-zero power of two (#214074)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 5 07:14:53 PDT 2026
Author: Adam Smith
Date: 2026-08-05T09:14:47-05:00
New Revision: 59de680df96e8ed4c283b792b9c1f77d05d071b2
URL: https://github.com/llvm/llvm-project/commit/59de680df96e8ed4c283b792b9c1f77d05d071b2
DIFF: https://github.com/llvm/llvm-project/commit/59de680df96e8ed4c283b792b9c1f77d05d071b2.diff
LOG: [CIR] Verify record_align is a non-zero power of two (#214074)
`#cir.record_layout` carries `record_align`, which CIRGen fills from
`ASTRecordLayout::getAlignment()` and consumers read as an
`llvm::Align`. That constructor asserts the value is a non-zero power of
two, so hand-written CIR naming any other alignment aborted the tool
rather than reporting a parse error. A zero tripped the non-zero assert
and a 3 tripped the power-of-two one, both inside `llvm::Align` with no
indication of which attribute was at fault.
Verify the field where it is parsed. Values CIRGen emits are already
well-formed, so this only affects hand-written input.
Assisted-by: Cursor / claude-opus-5
Added:
clang/test/CIR/IR/invalid-record-layout.cir
Modified:
clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Removed:
################################################################################
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
index a0fe997156a69..71585cd83fb66 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
@@ -123,7 +123,13 @@ def CIR_ArgPassingKind : CIR_I32EnumAttr<
let genSpecializedAttr = 0;
}
-def CIR_RecordLayoutAttr : CIR_Attr<"RecordLayout", "record_layout"> {
+def CIR_RecordLayoutAttr : CIR_Attr<"RecordLayout", "record_layout", [
+ // record_align is consumed as an llvm::Align, whose constructor asserts a
+ // non-zero power of two.
+ PredAttrTrait<"record_align must be a non-zero power of two",
+ CPred<"$record_align != 0 && "
+ "::llvm::isPowerOf2_64($record_align)">>
+ ]> {
let summary = "ABI layout metadata for a record type";
let description = [{
Holds AST-derived ABI metadata for a named record type. These
diff --git a/clang/test/CIR/IR/invalid-record-layout.cir b/clang/test/CIR/IR/invalid-record-layout.cir
new file mode 100644
index 0000000000000..61ebc9b37528c
--- /dev/null
+++ b/clang/test/CIR/IR/invalid-record-layout.cir
@@ -0,0 +1,19 @@
+// RUN: cir-opt %s -verify-diagnostics -split-input-file
+
+module attributes {
+ cir.record_layouts = {
+ // expected-error @below {{failed to verify that record_align must be a non-zero power of two}}
+ S = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+ has_trivial_dtor = true, record_align = 0>}
+} {
+}
+
+// -----
+
+module attributes {
+ cir.record_layouts = {
+ // expected-error @below {{failed to verify that record_align must be a non-zero power of two}}
+ S = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+ has_trivial_dtor = true, record_align = 3>}
+} {
+}
More information about the cfe-commits
mailing list