Skip to content

Commit 5f9ae61

Browse files
authored
[Support][YamlTraits] Add quoting for keys in textual YAML representation (#88763)
The support library contains helpers to parse and emit YAML documents. In the textual YAML representation, some strings need to be quoted, e.g. when containing unprintable characters. We already have such quoting implemented for YAML values. This patch applies the same quoting to YAML *keys*. One affected case is output of control registers in AMDGPU Msgpack metadata, which are printed in a format like this: ``` 0x2cca (SPI_SHADER_PGM_RSRC1_ES): 42 ``` With this patch, the key is quoted: ``` '0x2cca (SPI_SHADER_PGM_RSRC1_ES)': 42 ``` Most test changes come from this pattern.
1 parent caa9026 commit 5f9ae61

28 files changed

+281
-185
lines changed

llvm/include/llvm/Support/YAMLTraits.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -671,20 +671,26 @@ inline bool isBool(StringRef S) {
671671
// (except for TAB #x9, LF #xA, and CR #xD which are allowed), DEL #x7F, the C1
672672
// control block #x80-#x9F (except for NEL #x85 which is allowed), the surrogate
673673
// block #xD800-#xDFFF, #xFFFE, and #xFFFF.
674-
inline QuotingType needsQuotes(StringRef S) {
674+
//
675+
// Some strings are valid YAML values even unquoted, but without quotes are
676+
// interpreted as non-string type, for instance null, boolean or numeric values.
677+
// If ForcePreserveAsString is set, such strings are quoted.
678+
inline QuotingType needsQuotes(StringRef S, bool ForcePreserveAsString = true) {
675679
if (S.empty())
676680
return QuotingType::Single;
677681

678682
QuotingType MaxQuotingNeeded = QuotingType::None;
679683
if (isSpace(static_cast<unsigned char>(S.front())) ||
680684
isSpace(static_cast<unsigned char>(S.back())))
681685
MaxQuotingNeeded = QuotingType::Single;
682-
if (isNull(S))
683-
MaxQuotingNeeded = QuotingType::Single;
684-
if (isBool(S))
685-
MaxQuotingNeeded = QuotingType::Single;
686-
if (isNumeric(S))
687-
MaxQuotingNeeded = QuotingType::Single;
686+
if (ForcePreserveAsString) {
687+
if (isNull(S))
688+
MaxQuotingNeeded = QuotingType::Single;
689+
if (isBool(S))
690+
MaxQuotingNeeded = QuotingType::Single;
691+
if (isNumeric(S))
692+
MaxQuotingNeeded = QuotingType::Single;
693+
}
688694

689695
// 7.3.3 Plain Style
690696
// Plain scalars must not begin with most indicators, as this would cause
@@ -1636,6 +1642,7 @@ class Output : public IO {
16361642

16371643
private:
16381644
void output(StringRef s);
1645+
void output(StringRef, QuotingType);
16391646
void outputUpToEndOfLine(StringRef s);
16401647
void newLineCheck(bool EmptySequence = false);
16411648
void outputNewLine();

llvm/lib/Support/YAMLTraits.cpp

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -718,40 +718,8 @@ void Output::scalarString(StringRef &S, QuotingType MustQuote) {
718718
outputUpToEndOfLine("''");
719719
return;
720720
}
721-
if (MustQuote == QuotingType::None) {
722-
// Only quote if we must.
723-
outputUpToEndOfLine(S);
724-
return;
725-
}
726-
727-
const char *const Quote = MustQuote == QuotingType::Single ? "'" : "\"";
728-
output(Quote); // Starting quote.
729-
730-
// When using double-quoted strings (and only in that case), non-printable characters may be
731-
// present, and will be escaped using a variety of unicode-scalar and special short-form
732-
// escapes. This is handled in yaml::escape.
733-
if (MustQuote == QuotingType::Double) {
734-
output(yaml::escape(S, /* EscapePrintable= */ false));
735-
outputUpToEndOfLine(Quote);
736-
return;
737-
}
738-
739-
unsigned i = 0;
740-
unsigned j = 0;
741-
unsigned End = S.size();
742-
const char *Base = S.data();
743-
744-
// When using single-quoted strings, any single quote ' must be doubled to be escaped.
745-
while (j < End) {
746-
if (S[j] == '\'') { // Escape quotes.
747-
output(StringRef(&Base[i], j - i)); // "flush".
748-
output(StringLiteral("''")); // Print it as ''
749-
i = j + 1;
750-
}
751-
++j;
752-
}
753-
output(StringRef(&Base[i], j - i));
754-
outputUpToEndOfLine(Quote); // Ending quote.
721+
output(S, MustQuote);
722+
outputUpToEndOfLine("");
755723
}
756724

757725
void Output::blockScalarString(StringRef &S) {
@@ -801,6 +769,46 @@ void Output::output(StringRef s) {
801769
Out << s;
802770
}
803771

772+
void Output::output(StringRef S, QuotingType MustQuote) {
773+
if (MustQuote == QuotingType::None) {
774+
// Only quote if we must.
775+
output(S);
776+
return;
777+
}
778+
779+
StringLiteral Quote = MustQuote == QuotingType::Single ? StringLiteral("'")
780+
: StringLiteral("\"");
781+
output(Quote); // Starting quote.
782+
783+
// When using double-quoted strings (and only in that case), non-printable
784+
// characters may be present, and will be escaped using a variety of
785+
// unicode-scalar and special short-form escapes. This is handled in
786+
// yaml::escape.
787+
if (MustQuote == QuotingType::Double) {
788+
output(yaml::escape(S, /* EscapePrintable= */ false));
789+
output(Quote);
790+
return;
791+
}
792+
793+
unsigned i = 0;
794+
unsigned j = 0;
795+
unsigned End = S.size();
796+
const char *Base = S.data();
797+
798+
// When using single-quoted strings, any single quote ' must be doubled to be
799+
// escaped.
800+
while (j < End) {
801+
if (S[j] == '\'') { // Escape quotes.
802+
output(StringRef(&Base[i], j - i)); // "flush".
803+
output(StringLiteral("''")); // Print it as ''
804+
i = j + 1;
805+
}
806+
++j;
807+
}
808+
output(StringRef(&Base[i], j - i));
809+
output(Quote); // Ending quote.
810+
}
811+
804812
void Output::outputUpToEndOfLine(StringRef s) {
805813
output(s);
806814
if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) &&
@@ -853,7 +861,7 @@ void Output::newLineCheck(bool EmptySequence) {
853861
}
854862

855863
void Output::paddedKey(StringRef key) {
856-
output(key);
864+
output(key, needsQuotes(key, false));
857865
output(":");
858866
const char *spaces = " ";
859867
if (key.size() < strlen(spaces))
@@ -872,7 +880,7 @@ void Output::flowKey(StringRef Key) {
872880
Column = ColumnAtMapFlowStart;
873881
output(" ");
874882
}
875-
output(Key);
883+
output(Key, needsQuotes(Key, false));
876884
output(": ");
877885
}
878886

llvm/test/CodeGen/AMDGPU/amdpal-callable.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ attributes #0 = { nounwind }
142142

143143
; GCN: amdpal.pipelines:
144144
; GCN-NEXT: - .registers:
145-
; GCN-NEXT: 0x2e12 (COMPUTE_PGM_RSRC1): 0xaf01ca{{$}}
146-
; GCN-NEXT: 0x2e13 (COMPUTE_PGM_RSRC2): 0x8001{{$}}
145+
; GCN-NEXT: '0x2e12 (COMPUTE_PGM_RSRC1)': 0xaf01ca{{$}}
146+
; GCN-NEXT: '0x2e13 (COMPUTE_PGM_RSRC2)': 0x8001{{$}}
147147
; GCN-NEXT: .shader_functions:
148148
; GCN-NEXT: dynamic_stack:
149149
; GCN-NEXT: .backend_stack_size: 0x10{{$}}

llvm/test/CodeGen/AMDGPU/amdpal-cs.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
; GCN-NEXT: .entry_point: cs_amdpal
1212
; GCN-NEXT: .scratch_memory_size: 0
1313
; GCN: .registers:
14-
; GCN-NEXT: 0x2e12 (COMPUTE_PGM_RSRC1):
15-
; GCN-NEXT: 0x2e13 (COMPUTE_PGM_RSRC2):
14+
; GCN-NEXT: '0x2e12 (COMPUTE_PGM_RSRC1)':
15+
; GCN-NEXT: '0x2e13 (COMPUTE_PGM_RSRC2)':
1616
; GCN-NEXT: ...
1717
; GCN-NEXT: .end_amdgpu_pal_metadata
1818
define amdgpu_cs half @cs_amdpal(half %arg0) {

llvm/test/CodeGen/AMDGPU/amdpal-es.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
; GCN-NEXT: .entry_point: es_amdpal
1111
; GCN-NEXT: .scratch_memory_size: 0
1212
; GCN: .registers:
13-
; GCN-NEXT: 0x2cca (SPI_SHADER_PGM_RSRC1_ES): 0
13+
; GCN-NEXT: '0x2cca (SPI_SHADER_PGM_RSRC1_ES)': 0
1414
; GCN-NEXT: ...
1515
; GCN-NEXT: .end_amdgpu_pal_metadata
1616
define amdgpu_es half @es_amdpal(half %arg0) {

llvm/test/CodeGen/AMDGPU/amdpal-gs.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
; GCN-NEXT: .entry_point: gs_amdpal
1212
; GCN-NEXT: .scratch_memory_size: 0
1313
; GCN: .registers:
14-
; GCN-NEXT: 0x2c8a (SPI_SHADER_PGM_RSRC1_GS): 0
14+
; GCN-NEXT: '0x2c8a (SPI_SHADER_PGM_RSRC1_GS)': 0
1515
; GCN-NEXT: ...
1616
; GCN-NEXT: .end_amdgpu_pal_metadata
1717
define amdgpu_gs half @gs_amdpal(half %arg0) {

llvm/test/CodeGen/AMDGPU/amdpal-hs.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
; GCN-NEXT: .entry_point: hs_amdpal
1212
; GCN-NEXT: .scratch_memory_size: 0
1313
; GCN: .registers:
14-
; GCN-NEXT: 0x2d0a (SPI_SHADER_PGM_RSRC1_HS): 0
14+
; GCN-NEXT: '0x2d0a (SPI_SHADER_PGM_RSRC1_HS)': 0
1515
; GCN-NEXT: ...
1616
; GCN-NEXT: .end_amdgpu_pal_metadata
1717
define amdgpu_hs half @hs_amdpal(half %arg0) {

llvm/test/CodeGen/AMDGPU/amdpal-ls.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
; GCN-NEXT: .entry_point: ls_amdpal
1111
; GCN-NEXT: .scratch_memory_size: 0
1212
; GCN: .registers:
13-
; GCN-NEXT: 0x2d4a (SPI_SHADER_PGM_RSRC1_LS): 0
13+
; GCN-NEXT: '0x2d4a (SPI_SHADER_PGM_RSRC1_LS)': 0
1414
; GCN-NEXT: ...
1515
; GCN-NEXT: .end_amdgpu_pal_metadata
1616
define amdgpu_ls half @ls_amdpal(half %arg0) {

llvm/test/CodeGen/AMDGPU/amdpal-msgpack-cs.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
; amdpal compute shader: check for 0x2e12 (COMPUTE_PGM_RSRC1) in pal metadata
66
; GCN-LABEL: {{^}}cs_amdpal:
77
; GCN: .amdgpu_pal_metadata
8-
; GCN: 0x2e12 (COMPUTE_PGM_RSRC1)
8+
; GCN: '0x2e12 (COMPUTE_PGM_RSRC1)'
99
define amdgpu_cs half @cs_amdpal(half %arg0) {
1010
%add = fadd half %arg0, 1.0
1111
ret half %add

llvm/test/CodeGen/AMDGPU/amdpal-msgpack-default.ll

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,64 @@
33
; RUN: llc -mtriple=amdgcn--amdpal -mcpu=gfx900 -verify-machineinstrs < %s | FileCheck -check-prefix=GFX9 -enable-var-scope %s
44

55
; amdpal compute shader: check for 0x2e12 (COMPUTE_PGM_RSRC1) in pal metadata
6-
; SI-DAG: 0x2e12 (COMPUTE_PGM_RSRC1): 0x2f0000{{$}}
7-
; VI-DAG: 0x2e12 (COMPUTE_PGM_RSRC1): 0x2f02c0{{$}}
8-
; GFX9-DAG: 0x2e12 (COMPUTE_PGM_RSRC1): 0x2f0000{{$}}
6+
; SI-DAG: '0x2e12 (COMPUTE_PGM_RSRC1)': 0x2f0000{{$}}
7+
; VI-DAG: '0x2e12 (COMPUTE_PGM_RSRC1)': 0x2f02c0{{$}}
8+
; GFX9-DAG: '0x2e12 (COMPUTE_PGM_RSRC1)': 0x2f0000{{$}}
99
define amdgpu_cs half @cs_amdpal(half %arg0) {
1010
%add = fadd half %arg0, 1.0
1111
ret half %add
1212
}
1313

1414
; amdpal evaluation shader: check for 0x2cca (SPI_SHADER_PGM_RSRC1_ES) in pal metadata
15-
; SI-DAG: 0x2cca (SPI_SHADER_PGM_RSRC1_ES): 0x2f0000{{$}}
16-
; VI-DAG: 0x2cca (SPI_SHADER_PGM_RSRC1_ES): 0x2f02c0{{$}}
17-
; GFX9-DAG: 0x2cca (SPI_SHADER_PGM_RSRC1_ES): 0x2f0000{{$}}
15+
; SI-DAG: '0x2cca (SPI_SHADER_PGM_RSRC1_ES)': 0x2f0000{{$}}
16+
; VI-DAG: '0x2cca (SPI_SHADER_PGM_RSRC1_ES)': 0x2f02c0{{$}}
17+
; GFX9-DAG: '0x2cca (SPI_SHADER_PGM_RSRC1_ES)': 0x2f0000{{$}}
1818
define amdgpu_es half @es_amdpal(half %arg0) {
1919
%add = fadd half %arg0, 1.0
2020
ret half %add
2121
}
2222

2323
; amdpal geometry shader: check for 0x2c8a (SPI_SHADER_PGM_RSRC1_GS) in pal metadata
24-
; SI-DAG: 0x2c8a (SPI_SHADER_PGM_RSRC1_GS): 0x2f0000{{$}}
25-
; VI-DAG: 0x2c8a (SPI_SHADER_PGM_RSRC1_GS): 0x2f02c0{{$}}
26-
; GFX9-DAG: 0x2c8a (SPI_SHADER_PGM_RSRC1_GS): 0x2f0000{{$}}
24+
; SI-DAG: '0x2c8a (SPI_SHADER_PGM_RSRC1_GS)': 0x2f0000{{$}}
25+
; VI-DAG: '0x2c8a (SPI_SHADER_PGM_RSRC1_GS)': 0x2f02c0{{$}}
26+
; GFX9-DAG: '0x2c8a (SPI_SHADER_PGM_RSRC1_GS)': 0x2f0000{{$}}
2727
define amdgpu_gs half @gs_amdpal(half %arg0) {
2828
%add = fadd half %arg0, 1.0
2929
ret half %add
3030
}
3131

3232
; amdpal hull shader: check for 0x2d0a (SPI_SHADER_PGM_RSRC1_HS) in pal metadata
33-
; SI-DAG: 0x2d0a (SPI_SHADER_PGM_RSRC1_HS): 0x2f0000{{$}}
34-
; VI-DAG: 0x2d0a (SPI_SHADER_PGM_RSRC1_HS): 0x2f02c0{{$}}
35-
; GFX9-DAG: 0x2d0a (SPI_SHADER_PGM_RSRC1_HS): 0x2f0000{{$}}
33+
; SI-DAG: '0x2d0a (SPI_SHADER_PGM_RSRC1_HS)': 0x2f0000{{$}}
34+
; VI-DAG: '0x2d0a (SPI_SHADER_PGM_RSRC1_HS)': 0x2f02c0{{$}}
35+
; GFX9-DAG: '0x2d0a (SPI_SHADER_PGM_RSRC1_HS)': 0x2f0000{{$}}
3636
define amdgpu_hs half @hs_amdpal(half %arg0) {
3737
%add = fadd half %arg0, 1.0
3838
ret half %add
3939
}
4040

4141
; amdpal load shader: check for 0x2d4a (SPI_SHADER_PGM_RSRC1_LS) in pal metadata
42-
; SI-DAG: 0x2d4a (SPI_SHADER_PGM_RSRC1_LS): 0x2f0000{{$}}
43-
; VI-DAG: 0x2d4a (SPI_SHADER_PGM_RSRC1_LS): 0x2f02c0{{$}}
44-
; GFX9-DAG: 0x2d4a (SPI_SHADER_PGM_RSRC1_LS): 0x2f0000{{$}}
42+
; SI-DAG: '0x2d4a (SPI_SHADER_PGM_RSRC1_LS)': 0x2f0000{{$}}
43+
; VI-DAG: '0x2d4a (SPI_SHADER_PGM_RSRC1_LS)': 0x2f02c0{{$}}
44+
; GFX9-DAG: '0x2d4a (SPI_SHADER_PGM_RSRC1_LS)': 0x2f0000{{$}}
4545
define amdgpu_ls half @ls_amdpal(half %arg0) {
4646
%add = fadd half %arg0, 1.0
4747
ret half %add
4848
}
4949

5050
; amdpal pixel shader: check for 0x2c0a (SPI_SHADER_PGM_RSRC1_PS) in pal metadata
5151
; below.
52-
; SI-DAG: 0x2c0a (SPI_SHADER_PGM_RSRC1_PS): 0x2f0000{{$}}
53-
; VI-DAG: 0x2c0a (SPI_SHADER_PGM_RSRC1_PS): 0x2f02c0{{$}}
54-
; GFX9-DAG: 0x2c0a (SPI_SHADER_PGM_RSRC1_PS): 0x2f0000{{$}}
52+
; SI-DAG: '0x2c0a (SPI_SHADER_PGM_RSRC1_PS)': 0x2f0000{{$}}
53+
; VI-DAG: '0x2c0a (SPI_SHADER_PGM_RSRC1_PS)': 0x2f02c0{{$}}
54+
; GFX9-DAG: '0x2c0a (SPI_SHADER_PGM_RSRC1_PS)': 0x2f0000{{$}}
5555
define amdgpu_ps half @ps_amdpal(half %arg0) {
5656
%add = fadd half %arg0, 1.0
5757
ret half %add
5858
}
5959

6060
; amdpal vertex shader: check for 45352 (SPI_SHADER_PGM_RSRC1_VS) in pal metadata
61-
; SI-DAG: 0x2c4a (SPI_SHADER_PGM_RSRC1_VS): 0x2f0000{{$}}
62-
; VI-DAG: 0x2c4a (SPI_SHADER_PGM_RSRC1_VS): 0x2f02c0{{$}}
63-
; GFX9-DAG: 0x2c4a (SPI_SHADER_PGM_RSRC1_VS): 0x2f0000{{$}}
61+
; SI-DAG: '0x2c4a (SPI_SHADER_PGM_RSRC1_VS)': 0x2f0000{{$}}
62+
; VI-DAG: '0x2c4a (SPI_SHADER_PGM_RSRC1_VS)': 0x2f02c0{{$}}
63+
; GFX9-DAG: '0x2c4a (SPI_SHADER_PGM_RSRC1_VS)': 0x2f0000{{$}}
6464
define amdgpu_vs half @vs_amdpal(half %arg0) {
6565
%add = fadd half %arg0, 1.0
6666
ret half %add
@@ -75,7 +75,7 @@ define amdgpu_vs half @vs_amdpal(half %arg0) {
7575
; - 0x123456789abcdef0
7676
; - 0xfedcba9876543210
7777
; .registers:
78-
; 0x2c0b (SPI_SHADER_PGM_RSRC2_PS): 0x42000000
78+
; '0x2c0b (SPI_SHADER_PGM_RSRC2_PS)': 0x42000000
7979
; ...
8080
; .end_amdgpu_pal_metadata
8181

0 commit comments

Comments
 (0)