Skip to content

Commit 997e5e8

Browse files
authored
[CodeGen] Add a flag to disable emitting block signature strings (llvm#96944)
Users who don't need the signature string to be emitted can use the flag to reduce code size. rdar://121933818
1 parent d1fbdde commit 997e5e8

File tree

8 files changed

+60
-15
lines changed

8 files changed

+60
-15
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,11 @@ New Compiler Flags
445445
- ``-Wc++2c-compat`` group was added to help migrating existing codebases
446446
to upcoming C++26.
447447

448+
- ``-fdisable-block-signature-string`` instructs clang not to emit the signature
449+
string for blocks. Disabling the string can potentially break existing code
450+
that relies on it. Users should carefully consider this possibiilty when using
451+
the flag.
452+
448453
Deprecated Compiler Flags
449454
-------------------------
450455

clang/docs/UsersManual.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,12 @@ Clang options that don't fit neatly into other categories.
913913
binary compatibility issues on older x86_64 targets, however, so use it with
914914
caution.
915915

916+
.. option:: -fdisable-block-signature-string
917+
918+
Instruct clang not to emit the signature string for blocks. Disabling the
919+
string can potentially break existing code that relies on it. Users should
920+
carefully consider this possibiilty when using the flag.
921+
916922
.. _configuration-files:
917923

918924
Configuration files

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ CODEGENOPT(NoImplicitFloat , 1, 0) ///< Set when -mno-implicit-float is enable
188188
CODEGENOPT(NullPointerIsValid , 1, 0) ///< Assume Null pointer deference is defined.
189189
CODEGENOPT(OpenCLCorrectlyRoundedDivSqrt, 1, 0) ///< -cl-fp32-correctly-rounded-divide-sqrt
190190
CODEGENOPT(HIPCorrectlyRoundedDivSqrt, 1, 1) ///< -fno-hip-fp32-correctly-rounded-divide-sqrt
191+
CODEGENOPT(DisableBlockSignatureString, 1, 0) ///< Set when -fdisable-block-signature-string is enabled.
191192
CODEGENOPT(HIPSaveKernelArgName, 1, 0) ///< Set when -fhip-kernel-arg-name is enabled.
192193
CODEGENOPT(UniqueInternalLinkageNames, 1, 0) ///< Internal Linkage symbols get unique names.
193194
CODEGENOPT(SplitMachineFunctions, 1, 0) ///< Split machine functions using profile information.

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3477,6 +3477,11 @@ defm objc_avoid_heapify_local_blocks : BoolFOption<"objc-avoid-heapify-local-blo
34773477
PosFlag<SetTrue, [], [ClangOption], "Try">,
34783478
NegFlag<SetFalse, [], [ClangOption], "Don't try">,
34793479
BothFlags<[], [CC1Option], " to avoid heapifying local blocks">>;
3480+
defm disable_block_signature_string : BoolFOption<"disable-block-signature-string",
3481+
CodeGenOpts<"DisableBlockSignatureString">, DefaultFalse,
3482+
PosFlag<SetTrue, [], [ClangOption], "Disable">,
3483+
NegFlag<SetFalse, [], [ClangOption], "Don't disable">,
3484+
BothFlags<[], [CC1Option], " block signature string)">>;
34803485

34813486
def fomit_frame_pointer : Flag<["-"], "fomit-frame-pointer">, Group<f_Group>,
34823487
Visibility<[ClangOption, FlangOption]>,

clang/lib/CodeGen/CGBlocks.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,15 @@ static std::string getBlockDescriptorName(const CGBlockInfo &BlockInfo,
121121
Name += "_";
122122
}
123123

124-
std::string TypeAtEncoding =
125-
CGM.getContext().getObjCEncodingForBlock(BlockInfo.getBlockExpr());
126-
/// Replace occurrences of '@' with '\1'. '@' is reserved on ELF platforms as
127-
/// a separator between symbol name and symbol version.
128-
std::replace(TypeAtEncoding.begin(), TypeAtEncoding.end(), '@', '\1');
124+
std::string TypeAtEncoding;
125+
126+
if (!CGM.getCodeGenOpts().DisableBlockSignatureString) {
127+
TypeAtEncoding =
128+
CGM.getContext().getObjCEncodingForBlock(BlockInfo.getBlockExpr());
129+
/// Replace occurrences of '@' with '\1'. '@' is reserved on ELF platforms
130+
/// as a separator between symbol name and symbol version.
131+
std::replace(TypeAtEncoding.begin(), TypeAtEncoding.end(), '@', '\1');
132+
}
129133
Name += "e" + llvm::to_string(TypeAtEncoding.size()) + "_" + TypeAtEncoding;
130134
Name += "l" + CGM.getObjCRuntime().getRCBlockLayoutStr(CGM, BlockInfo);
131135
return Name;
@@ -201,9 +205,13 @@ static llvm::Constant *buildBlockDescriptor(CodeGenModule &CGM,
201205
}
202206

203207
// Signature. Mandatory ObjC-style method descriptor @encode sequence.
204-
std::string typeAtEncoding =
205-
CGM.getContext().getObjCEncodingForBlock(blockInfo.getBlockExpr());
206-
elements.add(CGM.GetAddrOfConstantCString(typeAtEncoding).getPointer());
208+
if (CGM.getCodeGenOpts().DisableBlockSignatureString) {
209+
elements.addNullPointer(i8p);
210+
} else {
211+
std::string typeAtEncoding =
212+
CGM.getContext().getObjCEncodingForBlock(blockInfo.getBlockExpr());
213+
elements.add(CGM.GetAddrOfConstantCString(typeAtEncoding).getPointer());
214+
}
207215

208216
// GC layout.
209217
if (C.getLangOpts().ObjC) {
@@ -814,7 +822,8 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {
814822
descriptor = buildBlockDescriptor(CGM, blockInfo);
815823

816824
// Compute the initial on-stack block flags.
817-
flags = BLOCK_HAS_SIGNATURE;
825+
if (!CGM.getCodeGenOpts().DisableBlockSignatureString)
826+
flags = BLOCK_HAS_SIGNATURE;
818827
if (blockInfo.HasCapturedVariableLayout)
819828
flags |= BLOCK_HAS_EXTENDED_LAYOUT;
820829
if (blockInfo.NeedsCopyDispose)
@@ -1300,7 +1309,9 @@ static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
13001309
fields.add(CGM.getNSConcreteGlobalBlock());
13011310

13021311
// __flags
1303-
BlockFlags flags = BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE;
1312+
BlockFlags flags = BLOCK_IS_GLOBAL;
1313+
if (!CGM.getCodeGenOpts().DisableBlockSignatureString)
1314+
flags |= BLOCK_HAS_SIGNATURE;
13041315
if (blockInfo.UsesStret)
13051316
flags |= BLOCK_USE_STRET;
13061317

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5751,6 +5751,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
57515751
Args.AddLastArg(CmdArgs, options::OPT_fexperimental_omit_vtable_rtti,
57525752
options::OPT_fno_experimental_omit_vtable_rtti);
57535753

5754+
Args.AddLastArg(CmdArgs, options::OPT_fdisable_block_signature_string,
5755+
options::OPT_fno_disable_block_signature_string);
5756+
57545757
// Handle segmented stacks.
57555758
Args.addOptInFlag(CmdArgs, options::OPT_fsplit_stack,
57565759
options::OPT_fno_split_stack);

clang/test/CodeGen/blocks.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
// RUN: %clang_cc1 -triple i386-unknown-unknown %s -emit-llvm -Wno-strict-prototypes -o - -fblocks | FileCheck %s
1+
// RUN: %clang_cc1 -triple i386-unknown-unknown %s -emit-llvm -Wno-strict-prototypes -o - -fblocks | FileCheck --check-prefix=CHECK --check-prefix=SIG_STR %s
2+
// RUN: %clang_cc1 -triple i386-unknown-unknown %s -emit-llvm -Wno-strict-prototypes -o - -fblocks -fdisable-block-signature-string | FileCheck --check-prefix=CHECK --check-prefix=NO_SIG_STR %s
23

3-
// CHECK: @{{.*}} = internal constant { i32, i32, ptr, ptr, ptr, ptr } { i32 0, i32 24, ptr @__copy_helper_block_4_20r, ptr @__destroy_helper_block_4_20r, ptr @{{.*}}, ptr null }, align 4
4-
// CHECK: @[[BLOCK_DESCRIPTOR_TMP21:.*]] = internal constant { i32, i32, ptr, ptr, ptr, ptr } { i32 0, i32 24, ptr @__copy_helper_block_4_20r, ptr @__destroy_helper_block_4_20r, ptr @{{.*}}, ptr null }, align 4
4+
// SIG_STR: @[[STR:.*]] = private unnamed_addr constant [6 x i8] c"v4@?0\00", align 1
5+
// SIG_STR: @{{.*}} = internal constant { ptr, i32, i32, ptr, ptr } { ptr @_NSConcreteGlobalBlock, i32 1342177280, i32 0, ptr @f_block_invoke, ptr @{{.*}} }, align 4
6+
// NO_SIG_STR: @{{.*}} = internal constant { ptr, i32, i32, ptr, ptr } { ptr @_NSConcreteGlobalBlock, i32 268435456, i32 0, ptr @f_block_invoke, ptr @{{.*}} }, align 4
7+
8+
// SIG_STR: @{{.*}} = internal constant { i32, i32, ptr, ptr, ptr, ptr } { i32 0, i32 24, ptr @__copy_helper_block_4_20r, ptr @__destroy_helper_block_4_20r, ptr @[[STR]], ptr null }, align 4
9+
// SIG_STR: @[[BLOCK_DESCRIPTOR_TMP21:.*]] = internal constant { i32, i32, ptr, ptr, ptr, ptr } { i32 0, i32 24, ptr @__copy_helper_block_4_20r, ptr @__destroy_helper_block_4_20r, ptr @[[STR]], ptr null }, align 4
10+
// NO_SIG_STR: @{{.*}} = internal constant { i32, i32, ptr, ptr, ptr, ptr } { i32 0, i32 24, ptr @__copy_helper_block_4_20r, ptr @__destroy_helper_block_4_20r, ptr null, ptr null }, align 4
11+
// NO_SIG_STR: @[[BLOCK_DESCRIPTOR_TMP21:.*]] = internal constant { i32, i32, ptr, ptr, ptr, ptr } { i32 0, i32 24, ptr @__copy_helper_block_4_20r, ptr @__destroy_helper_block_4_20r, ptr null, ptr null }, align 4
512

613
void (^f)(void) = ^{};
714

clang/test/CodeGenObjC/blocks.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
// RUN: %clang_cc1 -triple i386-apple-darwin9 -fobjc-runtime=macosx-fragile-10.5 -emit-llvm -fblocks -Wno-strict-prototypes -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -triple i386-apple-darwin9 -fobjc-runtime=macosx-fragile-10.5 -emit-llvm -fblocks -Wno-strict-prototypes -o - %s | FileCheck --check-prefix=CHECK --check-prefix=SIG_STR %s
2+
// RUN: %clang_cc1 -triple i386-apple-darwin9 -fobjc-runtime=macosx-fragile-10.5 -emit-llvm -fblocks -Wno-strict-prototypes -fdisable-block-signature-string -o - %s | FileCheck --check-prefix=CHECK --check-prefix=NO_SIG_STR %s
23

34
// Check that there is only one capture (20o) in the copy/dispose function
45
// names.
56

6-
// CHECK: @[[BLOCK_DESCRIPTOR0:.*]] = linkonce_odr hidden unnamed_addr constant { i32, i32, ptr, ptr, ptr, i32 } { i32 0, i32 28, ptr @__copy_helper_block_4_20o, ptr @__destroy_helper_block_4_20o, ptr @{{.*}}, i32 512 },
7+
// SIG_STR: @[[STR3:.*]] = private unnamed_addr constant [6 x i8] c"v4@?0\00", align 1
8+
// SIG_STR: @[[BLOCK_DESCRIPTOR0:"__block_descriptor_28_4_20o_e5_v4\\01\?0l"]] = linkonce_odr hidden unnamed_addr constant { i32, i32, ptr, ptr, ptr, i32 } { i32 0, i32 28, ptr @__copy_helper_block_4_20o, ptr @__destroy_helper_block_4_20o, ptr @[[STR3]], i32 512 },
9+
// NO_SIG_STR: @[[BLOCK_DESCRIPTOR0:__block_descriptor_28_4_20o_e0_l]] = linkonce_odr hidden unnamed_addr constant { i32, i32, ptr, ptr, ptr, i32 } { i32 0, i32 28, ptr @__copy_helper_block_4_20o, ptr @__destroy_helper_block_4_20o, ptr null, i32 512 },
10+
711

812
void (^gb0)(void);
913

@@ -132,6 +136,9 @@ void test5(A *a) {
132136
}
133137

134138
// CHECK-LABEL: define void @test5(
139+
// CHECK: %[[FLAGS:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr, ptr }>, ptr %{{.*}}, i32 0, i32 1
140+
// SIG_STR: store i32 -1040187392, ptr %[[FLAGS]], align 4
141+
// NO_SIG_STR: store i32 -2113929216, ptr %[[FLAGS]], align 4
135142
// CHECK: %[[V0:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr, ptr }>, ptr %{{.*}}, i32 0, i32 4
136143
// CHECK: store ptr @[[BLOCK_DESCRIPTOR0]], ptr %[[V0]],
137144

0 commit comments

Comments
 (0)