[clang] [Clang FE] Extensions for -ftrapv-handler call. (PR #232847)
Jonas Paulsson via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 16 10:49:40 PDT 2026
https://github.com/JonPsson1 updated https://github.com/llvm/llvm-project/pull/232847
>From c9b3be1cff1c62210b0bb3c20729fbca642febc1 Mon Sep 17 00:00:00 2001
From: Jonas Paulsson <paulson1 at linux.ibm.com>
Date: Fri, 16 Oct 2026 18:48:34 +0200
Subject: [PATCH 1/2] Extensions for -ftrapv-handler call.
---
clang/lib/CodeGen/CGExprScalar.cpp | 10 ++++++++++
clang/test/CodeGen/SystemZ/ftrapv-handler-argext.c | 11 +++++++++++
2 files changed, 21 insertions(+)
create mode 100644 clang/test/CodeGen/SystemZ/ftrapv-handler-argext.c
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index db2b7a4ad1fce2..e5c1ada3df5eb2 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -36,6 +36,7 @@
#include "clang/CodeGenUtils/ExprUtils.h"
#include "llvm/ADT/APFixedPoint.h"
#include "llvm/ADT/ScopeExit.h"
+#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/CFG.h"
@@ -4487,6 +4488,15 @@ Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
llvm::FunctionType::get(CGF.Int64Ty, argTypes, true);
llvm::FunctionCallee handler =
CGF.CGM.CreateRuntimeFunction(handlerTy, *handlerName);
+ // Extend the two unsigned i8 arguments (operation code and operand
+ // bit-width) if required by target.
+ llvm::Attribute::AttrKind I8ExtAttr =
+ llvm::TargetLibraryInfo::getExtAttrForI8Param(/*Signed=*/false);
+ if (I8ExtAttr != llvm::Attribute::AttrKind::None)
+ if (llvm::Function *F = dyn_cast<llvm::Function>(handler.getCallee())) {
+ F->addParamAttr(2, I8ExtAttr);
+ F->addParamAttr(3, I8ExtAttr);
+ }
// Sign extend the args to 64-bit, so that we can use the same handler for
// all types of overflow.
diff --git a/clang/test/CodeGen/SystemZ/ftrapv-handler-argext.c b/clang/test/CodeGen/SystemZ/ftrapv-handler-argext.c
new file mode 100644
index 00000000000000..e8e2e712998e07
--- /dev/null
+++ b/clang/test/CodeGen/SystemZ/ftrapv-handler-argext.c
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -triple s390x-linux-gnu -emit-llvm -o - -ftrapv \
+// RUN: -ftrapv-handler myhandler %s | FileCheck %s
+//
+// Check that the two i8 arguments of the -ftrapv-handler function (operation
+// code and operand bit-width) carry zeroext.
+//
+// CHECK: declare i64 @myhandler(i64, i64, i8 zeroext, i8 zeroext, ...)
+
+void test(int a, int b) {
+ (void)(a + b);
+}
>From 70415ee87c024e70e582d5f2ec3a0b8b864cf326 Mon Sep 17 00:00:00 2001
From: Jonas Paulsson <paulson1 at linux.ibm.com>
Date: Fri, 16 Oct 2026 19:48:42 +0200
Subject: [PATCH 2/2] Let CreateRuntimeFunction() do the work.
---
clang/lib/CodeGen/CGExprScalar.cpp | 19 ++++---------------
.../CodeGen/SystemZ/ftrapv-handler-argext.c | 2 +-
2 files changed, 5 insertions(+), 16 deletions(-)
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index e5c1ada3df5eb2..f1394fc5246459 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -36,7 +36,6 @@
#include "clang/CodeGenUtils/ExprUtils.h"
#include "llvm/ADT/APFixedPoint.h"
#include "llvm/ADT/ScopeExit.h"
-#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/CFG.h"
@@ -4482,21 +4481,11 @@ Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
Builder.SetInsertPoint(overflowBB);
// Get the overflow handler.
- llvm::Type *Int8Ty = CGF.Int8Ty;
- llvm::Type *argTypes[] = { CGF.Int64Ty, CGF.Int64Ty, Int8Ty, Int8Ty };
- llvm::FunctionType *handlerTy =
- llvm::FunctionType::get(CGF.Int64Ty, argTypes, true);
+ ASTContext &Ctx = CGF.getContext();
+ QualType ArgTys[] = {Ctx.LongLongTy, Ctx.LongLongTy, Ctx.UnsignedCharTy,
+ Ctx.UnsignedCharTy};
llvm::FunctionCallee handler =
- CGF.CGM.CreateRuntimeFunction(handlerTy, *handlerName);
- // Extend the two unsigned i8 arguments (operation code and operand
- // bit-width) if required by target.
- llvm::Attribute::AttrKind I8ExtAttr =
- llvm::TargetLibraryInfo::getExtAttrForI8Param(/*Signed=*/false);
- if (I8ExtAttr != llvm::Attribute::AttrKind::None)
- if (llvm::Function *F = dyn_cast<llvm::Function>(handler.getCallee())) {
- F->addParamAttr(2, I8ExtAttr);
- F->addParamAttr(3, I8ExtAttr);
- }
+ CGF.CGM.CreateRuntimeFunction(Ctx.LongLongTy, ArgTys, *handlerName);
// Sign extend the args to 64-bit, so that we can use the same handler for
// all types of overflow.
diff --git a/clang/test/CodeGen/SystemZ/ftrapv-handler-argext.c b/clang/test/CodeGen/SystemZ/ftrapv-handler-argext.c
index e8e2e712998e07..4b640c0459056d 100644
--- a/clang/test/CodeGen/SystemZ/ftrapv-handler-argext.c
+++ b/clang/test/CodeGen/SystemZ/ftrapv-handler-argext.c
@@ -4,7 +4,7 @@
// Check that the two i8 arguments of the -ftrapv-handler function (operation
// code and operand bit-width) carry zeroext.
//
-// CHECK: declare i64 @myhandler(i64, i64, i8 zeroext, i8 zeroext, ...)
+// CHECK: declare i64 @myhandler(i64 noundef, i64 noundef, i8 noundef zeroext, i8 noundef zeroext)
void test(int a, int b) {
(void)(a + b);
More information about the cfe-commits
mailing list