Skip to content

Commit db1e72d

Browse files
ahatanakahatanaka
authored andcommitted
[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 (cherry picked from commit 997e5e8)
1 parent 8131794 commit db1e72d

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
@@ -410,6 +410,11 @@ New Compiler Flags
410410
Matches MSVC behaviour by defining ``__STDC__`` to ``1`` when
411411
MSVC compatibility mode is used. It has no effect for C++ code.
412412

413+
- ``-fdisable-block-signature-string`` instructs clang not to emit the signature
414+
string for blocks. Disabling the string can potentially break existing code
415+
that relies on it. Users should carefully consider this possibiilty when using
416+
the flag.
417+
413418
Deprecated Compiler Flags
414419
-------------------------
415420

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
@@ -3510,6 +3510,11 @@ defm objc_avoid_heapify_local_blocks : BoolFOption<"objc-avoid-heapify-local-blo
35103510
PosFlag<SetTrue, [], [ClangOption], "Try">,
35113511
NegFlag<SetFalse, [], [ClangOption], "Don't try">,
35123512
BothFlags<[], [CC1Option], " to avoid heapifying local blocks">>;
3513+
defm disable_block_signature_string : BoolFOption<"disable-block-signature-string",
3514+
CodeGenOpts<"DisableBlockSignatureString">, DefaultFalse,
3515+
PosFlag<SetTrue, [], [ClangOption], "Disable">,
3516+
NegFlag<SetFalse, [], [ClangOption], "Don't disable">,
3517+
BothFlags<[], [CC1Option], " block signature string)">>;
35133518

35143519
def fomit_frame_pointer : Flag<["-"], "fomit-frame-pointer">, Group<f_Group>,
35153520
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;
@@ -204,9 +208,13 @@ static llvm::Constant *buildBlockDescriptor(CodeGenModule &CGM,
204208
}
205209

206210
// Signature. Mandatory ObjC-style method descriptor @encode sequence.
207-
std::string typeAtEncoding =
208-
CGM.getContext().getObjCEncodingForBlock(blockInfo.getBlockExpr());
209-
elements.add(CGM.GetAddrOfConstantCString(typeAtEncoding).getPointer());
211+
if (CGM.getCodeGenOpts().DisableBlockSignatureString) {
212+
elements.addNullPointer(i8p);
213+
} else {
214+
std::string typeAtEncoding =
215+
CGM.getContext().getObjCEncodingForBlock(blockInfo.getBlockExpr());
216+
elements.add(CGM.GetAddrOfConstantCString(typeAtEncoding).getPointer());
217+
}
210218

211219
// GC layout.
212220
if (C.getLangOpts().ObjC) {
@@ -817,7 +825,8 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {
817825
descriptor = buildBlockDescriptor(CGM, blockInfo);
818826

819827
// Compute the initial on-stack block flags.
820-
flags = BLOCK_HAS_SIGNATURE;
828+
if (!CGM.getCodeGenOpts().DisableBlockSignatureString)
829+
flags = BLOCK_HAS_SIGNATURE;
821830
if (blockInfo.HasCapturedVariableLayout)
822831
flags |= BLOCK_HAS_EXTENDED_LAYOUT;
823832
if (blockInfo.NeedsCopyDispose)
@@ -1328,7 +1337,9 @@ static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
13281337
fields.add(CGM.getNSConcreteGlobalBlock());
13291338

13301339
// __flags
1331-
BlockFlags flags = BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE;
1340+
BlockFlags flags = BLOCK_IS_GLOBAL;
1341+
if (!CGM.getCodeGenOpts().DisableBlockSignatureString)
1342+
flags |= BLOCK_HAS_SIGNATURE;
13321343
if (blockInfo.UsesStret)
13331344
flags |= BLOCK_USE_STRET;
13341345

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5875,6 +5875,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &Job,
58755875
Args.AddLastArg(CmdArgs, options::OPT_fexperimental_omit_vtable_rtti,
58765876
options::OPT_fno_experimental_omit_vtable_rtti);
58775877

5878+
Args.AddLastArg(CmdArgs, options::OPT_fdisable_block_signature_string,
5879+
options::OPT_fno_disable_block_signature_string);
5880+
58785881
// Handle segmented stacks.
58795882
Args.addOptInFlag(CmdArgs, options::OPT_fsplit_stack,
58805883
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)