[clang] [llvm] [Clang] Fix argument extensions in CGBlocks.cpp (PR #111740)

Jonas Paulsson via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 9 11:54:08 PDT 2024


https://github.com/JonPsson1 created https://github.com/llvm/llvm-project/pull/111740

Add extension attributes in declarations of _Block_object_dispose and _Block_object_assign.

In order to make this one-liners wherever needed, a new casting method FunctionCallee::getAsFunction() has been added.

A problem is that it seems theoretically possible that the created Function is any other named Value in the module... Would it be ok to have the cast fail in such a case?

I realize this is not the approach you had in mind, but it would at least be nice to hear the reasoning as to why something more elaborate would be preferred?

Please help with the signedness for the _Block_object_dispose argument, which I have no clue about.

@efriedma-quic @nikic 


>From fb8b76aca00b97166e56fcaefc241850b2535f94 Mon Sep 17 00:00:00 2001
From: Jonas Paulsson <paulson1 at linux.ibm.com>
Date: Wed, 9 Oct 2024 18:51:22 +0200
Subject: [PATCH] Add SExt attr to 2nd arg

---
 clang/lib/CodeGen/CGBlocks.cpp      | 3 +++
 llvm/include/llvm/IR/DerivedTypes.h | 3 +++
 llvm/lib/IR/Type.cpp                | 8 ++++++++
 3 files changed, 14 insertions(+)

diff --git a/clang/lib/CodeGen/CGBlocks.cpp b/clang/lib/CodeGen/CGBlocks.cpp
index 684fda74407313..0f71163b565d6e 100644
--- a/clang/lib/CodeGen/CGBlocks.cpp
+++ b/clang/lib/CodeGen/CGBlocks.cpp
@@ -2842,6 +2842,8 @@ llvm::FunctionCallee CodeGenModule::getBlockObjectDispose() {
   llvm::FunctionType *fty
     = llvm::FunctionType::get(VoidTy, args, false);
   BlockObjectDispose = CreateRuntimeFunction(fty, "_Block_object_dispose");
+  // FIXME: Correct signedness of extension??
+  BlockObjectDispose.getAsFunction()->addParamAttr(1, llvm::Attribute::SExt);
   configureBlocksRuntimeObject(
       *this, cast<llvm::Constant>(BlockObjectDispose.getCallee()));
   return BlockObjectDispose;
@@ -2855,6 +2857,7 @@ llvm::FunctionCallee CodeGenModule::getBlockObjectAssign() {
   llvm::FunctionType *fty
     = llvm::FunctionType::get(VoidTy, args, false);
   BlockObjectAssign = CreateRuntimeFunction(fty, "_Block_object_assign");
+  BlockObjectAssign.getAsFunction()->addParamAttr(2, llvm::Attribute::SExt);
   configureBlocksRuntimeObject(
       *this, cast<llvm::Constant>(BlockObjectAssign.getCallee()));
   return BlockObjectAssign;
diff --git a/llvm/include/llvm/IR/DerivedTypes.h b/llvm/include/llvm/IR/DerivedTypes.h
index a24801d8bdf834..efacca6d773abf 100644
--- a/llvm/include/llvm/IR/DerivedTypes.h
+++ b/llvm/include/llvm/IR/DerivedTypes.h
@@ -29,6 +29,7 @@
 
 namespace llvm {
 
+class Function;
 class Value;
 class APInt;
 class LLVMContext;
@@ -189,6 +190,8 @@ class FunctionCallee {
 
   explicit operator bool() { return Callee; }
 
+  Function *getAsFunction();
+
 private:
   FunctionType *FnTy = nullptr;
   Value *Callee = nullptr;
diff --git a/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp
index f618263f79c313..2252e4fb39fc6a 100644
--- a/llvm/lib/IR/Type.cpp
+++ b/llvm/lib/IR/Type.cpp
@@ -361,6 +361,14 @@ bool FunctionType::isValidArgumentType(Type *ArgTy) {
   return ArgTy->isFirstClassType();
 }
 
+//===----------------------------------------------------------------------===//
+//                       FunctionCallee Implementation
+//===----------------------------------------------------------------------===//
+
+Function* FunctionCallee::getAsFunction() {
+  return cast<llvm::Function>(Callee);
+}
+
 //===----------------------------------------------------------------------===//
 //                       StructType Implementation
 //===----------------------------------------------------------------------===//



More information about the cfe-commits mailing list