Skip to content

Commit e64c9d1

Browse files
authored
[Transforms] Allow non-regex Source in SymbolRewriter in case of using ExplicitRewriteDescriptor (#154319)
Do not check that Source is a valid regex in case of Target (explicit) transformation. Source may contain special symbols that may cause an incorrect `invalid regex` error. Note that source and exactly one of [Target, Transform] must be provided. `Target (explicit transformation)`: In this kind of rule `Source` is treated as a symbol name and is matched in its entirety. `Target` field will denote the symbol name to transform to. `Transform (pattern transformation)`: This rule treats `Source` as a regex that should match the complete symbol name. `Transform` is a regex specifying the name to transform to.
1 parent 9b0b238 commit e64c9d1

File tree

3 files changed

+70
-30
lines changed

3 files changed

+70
-30
lines changed

llvm/lib/Transforms/Utils/SymbolRewriter.cpp

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,7 @@ parseRewriteFunctionDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
349349

350350
KeyValue = Key->getValue(KeyStorage);
351351
if (KeyValue == "source") {
352-
std::string Error;
353-
354352
Source = std::string(Value->getValue(ValueStorage));
355-
if (!Regex(Source).isValid(Error)) {
356-
YS.printError(Field.getKey(), "invalid regex: " + Error);
357-
return false;
358-
}
359353
} else if (KeyValue == "target") {
360354
Target = std::string(Value->getValue(ValueStorage));
361355
} else if (KeyValue == "transform") {
@@ -379,12 +373,22 @@ parseRewriteFunctionDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
379373

380374
// TODO see if there is a more elegant solution to selecting the rewrite
381375
// descriptor type
382-
if (!Target.empty())
376+
if (!Target.empty()) {
383377
DL->push_back(std::make_unique<ExplicitRewriteFunctionDescriptor>(
384378
Source, Target, Naked));
385-
else
386-
DL->push_back(
387-
std::make_unique<PatternRewriteFunctionDescriptor>(Source, Transform));
379+
return true;
380+
}
381+
382+
{
383+
std::string Error;
384+
if (!Regex(Source).isValid(Error)) {
385+
YS.printError(Descriptor, "invalid Source regex: " + Error);
386+
return false;
387+
}
388+
}
389+
390+
DL->push_back(
391+
std::make_unique<PatternRewriteFunctionDescriptor>(Source, Transform));
388392

389393
return true;
390394
}
@@ -418,13 +422,7 @@ parseRewriteGlobalVariableDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
418422

419423
KeyValue = Key->getValue(KeyStorage);
420424
if (KeyValue == "source") {
421-
std::string Error;
422-
423425
Source = std::string(Value->getValue(ValueStorage));
424-
if (!Regex(Source).isValid(Error)) {
425-
YS.printError(Field.getKey(), "invalid regex: " + Error);
426-
return false;
427-
}
428426
} else if (KeyValue == "target") {
429427
Target = std::string(Value->getValue(ValueStorage));
430428
} else if (KeyValue == "transform") {
@@ -441,13 +439,23 @@ parseRewriteGlobalVariableDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
441439
return false;
442440
}
443441

444-
if (!Target.empty())
442+
if (!Target.empty()) {
445443
DL->push_back(std::make_unique<ExplicitRewriteGlobalVariableDescriptor>(
446444
Source, Target,
447445
/*Naked*/ false));
448-
else
449-
DL->push_back(std::make_unique<PatternRewriteGlobalVariableDescriptor>(
450-
Source, Transform));
446+
return true;
447+
}
448+
449+
{
450+
std::string Error;
451+
if (!Regex(Source).isValid(Error)) {
452+
YS.printError(Descriptor, "invalid Source regex: " + Error);
453+
return false;
454+
}
455+
}
456+
457+
DL->push_back(std::make_unique<PatternRewriteGlobalVariableDescriptor>(
458+
Source, Transform));
451459

452460
return true;
453461
}
@@ -481,13 +489,7 @@ parseRewriteGlobalAliasDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
481489

482490
KeyValue = Key->getValue(KeyStorage);
483491
if (KeyValue == "source") {
484-
std::string Error;
485-
486492
Source = std::string(Value->getValue(ValueStorage));
487-
if (!Regex(Source).isValid(Error)) {
488-
YS.printError(Field.getKey(), "invalid regex: " + Error);
489-
return false;
490-
}
491493
} else if (KeyValue == "target") {
492494
Target = std::string(Value->getValue(ValueStorage));
493495
} else if (KeyValue == "transform") {
@@ -504,13 +506,23 @@ parseRewriteGlobalAliasDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
504506
return false;
505507
}
506508

507-
if (!Target.empty())
509+
if (!Target.empty()) {
508510
DL->push_back(std::make_unique<ExplicitRewriteNamedAliasDescriptor>(
509511
Source, Target,
510512
/*Naked*/ false));
511-
else
512-
DL->push_back(std::make_unique<PatternRewriteNamedAliasDescriptor>(
513-
Source, Transform));
513+
return true;
514+
}
515+
516+
{
517+
std::string Error;
518+
if (!Regex(Source).isValid(Error)) {
519+
YS.printError(Descriptor, "invalid Source regex: " + Error);
520+
return false;
521+
}
522+
}
523+
524+
DL->push_back(
525+
std::make_unique<PatternRewriteNamedAliasDescriptor>(Source, Transform));
514526

515527
return true;
516528
}

llvm/test/SymbolRewriter/rewrite.ll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ $source_comdat_variable = comdat largest
4646
$source_comdat_variable_1 = comdat nodeduplicate
4747
@source_comdat_variable_1 = global i32 64, comdat($source_comdat_variable_1)
4848

49+
declare void @"?source_bad_regex_function"()
50+
51+
@"?source_bad_regex_variable" = external global i32
52+
53+
@"?source_bad_regex_alias" = alias void (ptr), ptr @_ZN1SC2Ev
54+
4955
; CHECK: $target_comdat_function = comdat any
5056
; CHECK: $target_comdat_function_1 = comdat exactmatch
5157
; CHECK: $target_comdat_variable = comdat largest
@@ -62,6 +68,12 @@ $source_comdat_variable_1 = comdat nodeduplicate
6268
; CHECK: @target_comdat_variable_1 = global i32 64, comdat
6369
; CHECK-NOT: @source_comdat_variable_1 = global i32 64, comdat
6470

71+
; CHECK: @target_bad_regex_variable = external global i32
72+
; CHECK-NOT: @"?source_bad_regex_variable" = external global i32
73+
74+
; CHECK: @target_bad_regex_alias = alias void (ptr), ptr @_ZN1SC2Ev
75+
; CHECK-NOT: @"?source_bad_regex_alias" = alias void (ptr), ptr @_ZN1SC2Ev
76+
6577
; CHECK: declare void @target_function()
6678
; CHECK-NOT: declare void @source_function()
6779
; CHECK: declare void @target_pattern_function()
@@ -90,3 +102,5 @@ $source_comdat_variable_1 = comdat nodeduplicate
90102
; CHECK: define dllexport void @target_comdat_function_1() comdat
91103
; CHECK-NOT: define dllexport void @source_comdat_function_1() comdat
92104

105+
; CHECK: declare void @target_bad_regex_function()
106+
; CHECK-NOT: declare void @"?source_bad_regex_function"()

llvm/test/SymbolRewriter/rewrite.map

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,17 @@ global variable: {
6464
transform: target_comdat_variable_\1,
6565
}
6666

67+
function: {
68+
source: ?source_bad_regex_function,
69+
target: target_bad_regex_function,
70+
}
71+
72+
global variable: {
73+
source: ?source_bad_regex_variable,
74+
target: target_bad_regex_variable,
75+
}
76+
77+
global alias: {
78+
source: ?source_bad_regex_alias,
79+
target: target_bad_regex_alias,
80+
}

0 commit comments

Comments
 (0)