[clang] [clang][X86] Use a sized byval type for an incomplete interrupt frame (PR #239937)

Feng Zou via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 15 18:21:56 PST 2026


https://github.com/fzou1 updated https://github.com/llvm/llvm-project/pull/239937

>From 8199ef3372db97a7e05c74d57682b08e9cd8e271 Mon Sep 17 00:00:00 2001
From: "Zou, Feng" <feng.zou at intel.com>
Date: Mon, 26 Oct 2026 04:20:17 +0530
Subject: [PATCH 1/2] [clang][X86] Use a sized byval type for an incomplete
 interrupt frame

x86_intrcc requires parameter 0 to be byval, and addX86InterruptAttrs
uses the frame's type as the byval type. When that type is incomplete,
as in GCC's documentation ("struct interrupt_frame;"), or a function
type, it is unsized, so the module fails verification ("Attribute
'byval' does not support unsized types") or, without the verifier,
instruction selection asserts when computing the type's size.

The backend places the frame at a fixed offset and uses the byval type
only to size its fixed stack object, so fall back to i8, the type
already used for "void *". Frame types that are complete when the
handler is emitted are unaffected; one completed only later in the
file, which used to keep its struct type, now also gets i8.

Assisted-by: Claude Opus 5.5 <noreply at anthropic.com>
---
 clang/lib/CodeGen/Targets/X86.cpp             |  5 +++
 .../attr-x86-interrupt-incomplete-frame.c     | 45 +++++++++++++++++++
 2 files changed, 50 insertions(+)
 create mode 100644 clang/test/CodeGen/attr-x86-interrupt-incomplete-frame.c

diff --git a/clang/lib/CodeGen/Targets/X86.cpp b/clang/lib/CodeGen/Targets/X86.cpp
index 5324c8d366b829..fc49756374fd80 100644
--- a/clang/lib/CodeGen/Targets/X86.cpp
+++ b/clang/lib/CodeGen/Targets/X86.cpp
@@ -1234,6 +1234,11 @@ static void addX86InterruptAttrs(const FunctionDecl *FD, llvm::GlobalValue *GV,
 
   const auto *PtrTy = FD->getParamDecl(0)->getType()->castAs<PointerType>();
   llvm::Type *ByValTy = CGM.getTypes().ConvertType(PtrTy->getPointeeType());
+  // byval needs a sized type, but the frame's type may be unsized, e.g. an
+  // incomplete struct ("struct interrupt_frame;" in GCC's documentation). The
+  // backend locates the frame itself, so use i8 as for "void *".
+  if (!ByValTy->isSized())
+    ByValTy = CGM.Int8Ty;
   llvm::Attribute NewAttr = llvm::Attribute::getWithByValType(
     Fn->getContext(), ByValTy);
   Fn->addParamAttr(0, NewAttr);
diff --git a/clang/test/CodeGen/attr-x86-interrupt-incomplete-frame.c b/clang/test/CodeGen/attr-x86-interrupt-incomplete-frame.c
new file mode 100644
index 00000000000000..f07c5307914834
--- /dev/null
+++ b/clang/test/CodeGen/attr-x86-interrupt-incomplete-frame.c
@@ -0,0 +1,45 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu %s -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,X86_64
+// RUN: %clang_cc1 -triple i386-unknown-linux-gnu %s -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,X86
+
+// The interrupt frame's type may be unsized: an incomplete type, as in GCC's
+// documentation, or a function type. byval needs a sized type, so i8 is used,
+// as for "void *".
+
+#ifdef __x86_64__
+typedef __UINT64_TYPE__ uword;
+#else
+typedef __UINT32_TYPE__ uword;
+#endif
+
+struct interrupt_frame;
+union frame_union;
+typedef struct frame_tag frame_t;
+
+// CHECK: define{{.*}} x86_intrcc void @frame_void(ptr noundef byval(i8) %{{.+}})
+__attribute__((interrupt)) void frame_void(void *f) {}
+
+// CHECK: define{{.*}} x86_intrcc void @frame_struct(ptr noundef byval(i8) %{{.+}})
+__attribute__((interrupt)) void frame_struct(struct interrupt_frame *f) {}
+
+// X86_64: define{{.*}} x86_intrcc void @frame_struct_ec(ptr noundef byval(i8) %{{.+}}, i64 noundef %{{.+}})
+// X86: define{{.*}} x86_intrcc void @frame_struct_ec(ptr noundef byval(i8) %{{.+}}, i32 noundef %{{.+}})
+__attribute__((interrupt)) void frame_struct_ec(struct interrupt_frame *f,
+                                                uword ec) {}
+
+// CHECK: define{{.*}} x86_intrcc void @frame_union(ptr noundef byval(i8) %{{.+}})
+__attribute__((interrupt)) void frame_union(union frame_union *f) {}
+
+// CHECK: define{{.*}} x86_intrcc void @frame_typedef(ptr noundef byval(i8) %{{.+}})
+__attribute__((interrupt)) void frame_typedef(frame_t *f) {}
+
+// CHECK: define{{.*}} x86_intrcc void @frame_func(ptr noundef byval(i8) %{{.+}})
+__attribute__((interrupt)) void frame_func(void (*f)(void)) {}
+
+// A type completed after the handler is still incomplete where the handler is
+// emitted.
+struct frame_later;
+// CHECK: define{{.*}} x86_intrcc void @frame_later(ptr noundef byval(i8) %{{.+}})
+__attribute__((interrupt)) void frame_later(struct frame_later *f) {}
+struct frame_later {
+  uword ip;
+};

>From 7cddf6514667b2c5a690d1f72de52c8c3577b42a Mon Sep 17 00:00:00 2001
From: "Zou, Feng" <feng.zou at intel.com>
Date: Thu, 5 Nov 2026 22:45:41 +0530
Subject: [PATCH 2/2] Use the backend's frame size as the byval type

byval(i8) claims a one-byte object, so optimizations may treat a read of
any later frame slot as undefined. The backend lays out the interrupt
frame as five stack slots (CC_X86_Intr), so use [5 x iN] when the
frame's type gives no size, for void as well as for unsized types.

Assisted-by: Claude Opus 5.5 <noreply at anthropic.com>
---
 clang/lib/CodeGen/Targets/X86.cpp             | 13 ++++++----
 .../attr-x86-interrupt-incomplete-frame.c     | 26 +++++++++----------
 2 files changed, 21 insertions(+), 18 deletions(-)

diff --git a/clang/lib/CodeGen/Targets/X86.cpp b/clang/lib/CodeGen/Targets/X86.cpp
index fc49756374fd80..e7f47f87d13b58 100644
--- a/clang/lib/CodeGen/Targets/X86.cpp
+++ b/clang/lib/CodeGen/Targets/X86.cpp
@@ -1234,11 +1234,14 @@ static void addX86InterruptAttrs(const FunctionDecl *FD, llvm::GlobalValue *GV,
 
   const auto *PtrTy = FD->getParamDecl(0)->getType()->castAs<PointerType>();
   llvm::Type *ByValTy = CGM.getTypes().ConvertType(PtrTy->getPointeeType());
-  // byval needs a sized type, but the frame's type may be unsized, e.g. an
-  // incomplete struct ("struct interrupt_frame;" in GCC's documentation). The
-  // backend locates the frame itself, so use i8 as for "void *".
-  if (!ByValTy->isSized())
-    ByValTy = CGM.Int8Ty;
+  // byval marks parameter 0 as the address of the frame the processor pushed,
+  // which the backend models as five stack slots (CC_X86_Intr). The byval type
+  // must not claim less than that: optimizations treat accesses past its end
+  // as undefined. Use the five slots when the frame's type gives no size: void,
+  // or unsized, e.g. an incomplete struct ("struct interrupt_frame;" in GCC's
+  // documentation).
+  if (PtrTy->getPointeeType()->isVoidType() || !ByValTy->isSized())
+    ByValTy = llvm::ArrayType::get(CGM.IntPtrTy, 5);
   llvm::Attribute NewAttr = llvm::Attribute::getWithByValType(
     Fn->getContext(), ByValTy);
   Fn->addParamAttr(0, NewAttr);
diff --git a/clang/test/CodeGen/attr-x86-interrupt-incomplete-frame.c b/clang/test/CodeGen/attr-x86-interrupt-incomplete-frame.c
index f07c5307914834..d96168e62880ae 100644
--- a/clang/test/CodeGen/attr-x86-interrupt-incomplete-frame.c
+++ b/clang/test/CodeGen/attr-x86-interrupt-incomplete-frame.c
@@ -1,9 +1,9 @@
-// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu %s -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,X86_64
-// RUN: %clang_cc1 -triple i386-unknown-linux-gnu %s -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,X86
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu %s -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,X86_64 -DFRAME="[5 x i64]"
+// RUN: %clang_cc1 -triple i386-unknown-linux-gnu %s -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,X86 -DFRAME="[5 x i32]"
 
-// The interrupt frame's type may be unsized: an incomplete type, as in GCC's
-// documentation, or a function type. byval needs a sized type, so i8 is used,
-// as for "void *".
+// When the frame's type gives no size (void, an incomplete type as in GCC's
+// documentation, or a function type), the byval type is the five stack slots
+// the backend assigns to the frame.
 
 #ifdef __x86_64__
 typedef __UINT64_TYPE__ uword;
@@ -15,30 +15,30 @@ struct interrupt_frame;
 union frame_union;
 typedef struct frame_tag frame_t;
 
-// CHECK: define{{.*}} x86_intrcc void @frame_void(ptr noundef byval(i8) %{{.+}})
+// CHECK: define{{.*}} x86_intrcc void @frame_void(ptr noundef byval([[FRAME]]) %{{.+}})
 __attribute__((interrupt)) void frame_void(void *f) {}
 
-// CHECK: define{{.*}} x86_intrcc void @frame_struct(ptr noundef byval(i8) %{{.+}})
+// CHECK: define{{.*}} x86_intrcc void @frame_struct(ptr noundef byval([[FRAME]]) %{{.+}})
 __attribute__((interrupt)) void frame_struct(struct interrupt_frame *f) {}
 
-// X86_64: define{{.*}} x86_intrcc void @frame_struct_ec(ptr noundef byval(i8) %{{.+}}, i64 noundef %{{.+}})
-// X86: define{{.*}} x86_intrcc void @frame_struct_ec(ptr noundef byval(i8) %{{.+}}, i32 noundef %{{.+}})
+// X86_64: define{{.*}} x86_intrcc void @frame_struct_ec(ptr noundef byval([[FRAME]]) %{{.+}}, i64 noundef %{{.+}})
+// X86: define{{.*}} x86_intrcc void @frame_struct_ec(ptr noundef byval([[FRAME]]) %{{.+}}, i32 noundef %{{.+}})
 __attribute__((interrupt)) void frame_struct_ec(struct interrupt_frame *f,
                                                 uword ec) {}
 
-// CHECK: define{{.*}} x86_intrcc void @frame_union(ptr noundef byval(i8) %{{.+}})
+// CHECK: define{{.*}} x86_intrcc void @frame_union(ptr noundef byval([[FRAME]]) %{{.+}})
 __attribute__((interrupt)) void frame_union(union frame_union *f) {}
 
-// CHECK: define{{.*}} x86_intrcc void @frame_typedef(ptr noundef byval(i8) %{{.+}})
+// CHECK: define{{.*}} x86_intrcc void @frame_typedef(ptr noundef byval([[FRAME]]) %{{.+}})
 __attribute__((interrupt)) void frame_typedef(frame_t *f) {}
 
-// CHECK: define{{.*}} x86_intrcc void @frame_func(ptr noundef byval(i8) %{{.+}})
+// CHECK: define{{.*}} x86_intrcc void @frame_func(ptr noundef byval([[FRAME]]) %{{.+}})
 __attribute__((interrupt)) void frame_func(void (*f)(void)) {}
 
 // A type completed after the handler is still incomplete where the handler is
 // emitted.
 struct frame_later;
-// CHECK: define{{.*}} x86_intrcc void @frame_later(ptr noundef byval(i8) %{{.+}})
+// CHECK: define{{.*}} x86_intrcc void @frame_later(ptr noundef byval([[FRAME]]) %{{.+}})
 __attribute__((interrupt)) void frame_later(struct frame_later *f) {}
 struct frame_later {
   uword ip;



More information about the cfe-commits mailing list