Skip to content

Commit 9f5756a

Browse files
committed
[MIR] Replace bespoke DIExpression parser
Resolve FIXME by using the LLParser implementation of parseDIExpression from the MIParser.
1 parent a8cabb6 commit 9f5756a

File tree

7 files changed

+177
-48
lines changed

7 files changed

+177
-48
lines changed

llvm/include/llvm/AsmParser/LLParser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ namespace llvm {
201201
bool parseTypeAtBeginning(Type *&Ty, unsigned &Read,
202202
const SlotMapping *Slots);
203203

204+
bool parseDIExpressionBodyAtBeginning(MDNode *&Result, unsigned &Read,
205+
const SlotMapping *Slots);
206+
204207
LLVMContext &getContext() { return Context; }
205208

206209
private:
@@ -591,6 +594,7 @@ namespace llvm {
591594
template <class ParserTy>
592595
bool parseMDFieldsImpl(ParserTy ParseField, LocTy &ClosingLoc);
593596
bool parseSpecializedMDNode(MDNode *&N, bool IsDistinct = false);
597+
bool parseDIExpressionBody(MDNode *&Result, bool IsDistinct);
594598

595599
#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
596600
bool parse##CLASS(MDNode *&Result, bool IsDistinct);

llvm/include/llvm/AsmParser/Parser.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
namespace llvm {
2222

2323
class Constant;
24+
class DIExpression;
2425
class LLVMContext;
2526
class MemoryBufferRef;
2627
class Module;
@@ -202,6 +203,11 @@ Type *parseType(StringRef Asm, SMDiagnostic &Err, const Module &M,
202203
Type *parseTypeAtBeginning(StringRef Asm, unsigned &Read, SMDiagnostic &Err,
203204
const Module &M, const SlotMapping *Slots = nullptr);
204205

206+
DIExpression *parseDIExpressionBodyAtBeginning(StringRef Asm, unsigned &Read,
207+
SMDiagnostic &Err,
208+
const Module &M,
209+
const SlotMapping *Slots);
210+
205211
} // End llvm namespace
206212

207213
#endif

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ bool LLParser::parseTypeAtBeginning(Type *&Ty, unsigned &Read,
124124
return false;
125125
}
126126

127+
bool LLParser::parseDIExpressionBodyAtBeginning(MDNode *&Result, unsigned &Read,
128+
const SlotMapping *Slots) {
129+
restoreParsingState(Slots);
130+
Lex.Lex();
131+
132+
Read = 0;
133+
SMLoc Start = Lex.getLoc();
134+
Result = nullptr;
135+
bool Status = parseDIExpressionBody(Result, /*IsDistinct=*/false);
136+
SMLoc End = Lex.getLoc();
137+
Read = End.getPointer() - Start.getPointer();
138+
139+
return Status;
140+
}
141+
127142
void LLParser::restoreParsingState(const SlotMapping *Slots) {
128143
if (!Slots)
129144
return;
@@ -5823,12 +5838,9 @@ bool LLParser::parseDILabel(MDNode *&Result, bool IsDistinct) {
58235838
return false;
58245839
}
58255840

5826-
/// parseDIExpression:
5827-
/// ::= !DIExpression(0, 7, -1)
5828-
bool LLParser::parseDIExpression(MDNode *&Result, bool IsDistinct) {
5829-
assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
5830-
Lex.Lex();
5831-
5841+
/// parseDIExpressionBody:
5842+
/// ::= (0, 7, -1)
5843+
bool LLParser::parseDIExpressionBody(MDNode *&Result, bool IsDistinct) {
58325844
if (parseToken(lltok::lparen, "expected '(' here"))
58335845
return true;
58345846

@@ -5871,6 +5883,16 @@ bool LLParser::parseDIExpression(MDNode *&Result, bool IsDistinct) {
58715883
return false;
58725884
}
58735885

5886+
/// parseDIExpression:
5887+
/// ::= !DIExpression(0, 7, -1)
5888+
bool LLParser::parseDIExpression(MDNode *&Result, bool IsDistinct) {
5889+
assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
5890+
assert(Lex.getStrVal() == "DIExpression" && "Expected '!DIExpression'");
5891+
Lex.Lex();
5892+
5893+
return parseDIExpressionBody(Result, IsDistinct);
5894+
}
5895+
58745896
/// ParseDIArgList:
58755897
/// ::= !DIArgList(i32 7, i64 %0)
58765898
bool LLParser::parseDIArgList(Metadata *&MD, PerFunctionState *PFS) {

llvm/lib/AsmParser/Parser.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "llvm/AsmParser/Parser.h"
1414
#include "llvm/AsmParser/LLParser.h"
15+
#include "llvm/IR/DebugInfoMetadata.h"
1516
#include "llvm/IR/Module.h"
1617
#include "llvm/IR/ModuleSummaryIndex.h"
1718
#include "llvm/Support/MemoryBuffer.h"
@@ -224,3 +225,18 @@ Type *llvm::parseTypeAtBeginning(StringRef Asm, unsigned &Read,
224225
return nullptr;
225226
return Ty;
226227
}
228+
229+
DIExpression *llvm::parseDIExpressionBodyAtBeginning(StringRef Asm,
230+
unsigned &Read,
231+
SMDiagnostic &Err,
232+
const Module &M,
233+
const SlotMapping *Slots) {
234+
SourceMgr SM;
235+
std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
236+
SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
237+
MDNode *MD;
238+
if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
239+
.parseDIExpressionBodyAtBeginning(MD, Read, Slots))
240+
return nullptr;
241+
return dyn_cast<DIExpression>(MD);
242+
}

llvm/lib/CodeGen/MIRParser/MIParser.cpp

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,48 +2302,14 @@ bool MIParser::parseMDNode(MDNode *&Node) {
23022302
}
23032303

23042304
bool MIParser::parseDIExpression(MDNode *&Expr) {
2305-
assert(Token.is(MIToken::md_diexpr));
2306-
lex();
2307-
2308-
// FIXME: Share this parsing with the IL parser.
2309-
SmallVector<uint64_t, 8> Elements;
2310-
2311-
if (expectAndConsume(MIToken::lparen))
2312-
return true;
2313-
2314-
if (Token.isNot(MIToken::rparen)) {
2315-
do {
2316-
if (Token.is(MIToken::Identifier)) {
2317-
if (unsigned Op = dwarf::getOperationEncoding(Token.stringValue())) {
2318-
lex();
2319-
Elements.push_back(Op);
2320-
continue;
2321-
}
2322-
if (unsigned Enc = dwarf::getAttributeEncoding(Token.stringValue())) {
2323-
lex();
2324-
Elements.push_back(Enc);
2325-
continue;
2326-
}
2327-
return error(Twine("invalid DWARF op '") + Token.stringValue() + "'");
2328-
}
2329-
2330-
if (Token.isNot(MIToken::IntegerLiteral) ||
2331-
Token.integerValue().isSigned())
2332-
return error("expected unsigned integer");
2333-
2334-
auto &U = Token.integerValue();
2335-
if (U.ugt(UINT64_MAX))
2336-
return error("element too large, limit is " + Twine(UINT64_MAX));
2337-
Elements.push_back(U.getZExtValue());
2338-
lex();
2339-
2340-
} while (consumeIfPresent(MIToken::comma));
2341-
}
2342-
2343-
if (expectAndConsume(MIToken::rparen))
2344-
return true;
2345-
2346-
Expr = DIExpression::get(MF.getFunction().getContext(), Elements);
2305+
unsigned Read;
2306+
Expr = llvm::parseDIExpressionBodyAtBeginning(
2307+
CurrentSource, Read, Error, *PFS.MF.getFunction().getParent(),
2308+
&PFS.IRSlots);
2309+
CurrentSource = CurrentSource.slice(Read, StringRef::npos);
2310+
lex();
2311+
if (!Expr)
2312+
return error(Error.getMessage());
23472313
return false;
23482314
}
23492315

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# RUN: sed -e 's/STACK_EXPR//g' -e 's/BODY_EXPR//g' %s | llc -x mir -run-pass none -o - 2>&1 | FileCheck --check-prefix=EXPR-NONE %s
2+
# RUN: sed -e 's/STACK_EXPR/DW_OP_LLVM_arg, 0, DW_OP_dup, DW_OP_plus/g' -e 's/BODY_EXPR/DW_OP_LLVM_arg, 0, DW_OP_dup, DW_OP_plus/g' %s \
3+
# RUN: | llc -x mir -run-pass none -o - 2>&1 | FileCheck --check-prefix=EXPR-SOME %s
4+
# RUN: sed -e 's/STACK_EXPR/DW_OP_foobar/g' -e 's/BODY_EXPR//g' %s | not llc -x mir -run-pass none -o - 2>&1 | FileCheck --check-prefix=EXPR-ERR-STACK-INVALID-OP %s
5+
# RUN: sed -e 's/STACK_EXPR//g' -e 's/BODY_EXPR/DW_OP_foobar/g' %s | not llc -x mir -run-pass none -o - 2>&1 | FileCheck --check-prefix=EXPR-ERR-BODY-INVALID-OP %s
6+
# RUN: sed -e 's/STACK_EXPR/DW_ATE_foobar/g' -e 's/BODY_EXPR//g' %s | not llc -x mir -run-pass none -o - 2>&1 | FileCheck --check-prefix=EXPR-ERR-STACK-INVALID-ATTR %s
7+
# RUN: sed -e 's/STACK_EXPR//g' -e 's/BODY_EXPR/DW_ATE_foobar/g' %s | not llc -x mir -run-pass none -o - 2>&1 | FileCheck --check-prefix=EXPR-ERR-BODY-INVALID-ATTR %s
8+
# RUN: sed -e 's/STACK_EXPR/foobar/g' -e 's/BODY_EXPR//g' %s | not llc -x mir -run-pass none -o - 2>&1 | FileCheck --check-prefix=EXPR-ERR-STACK-INVALID-INT %s
9+
# RUN: sed -e 's/STACK_EXPR//g' -e 's/BODY_EXPR/foobar/g' %s | not llc -x mir -run-pass none -o - 2>&1 | FileCheck --check-prefix=EXPR-ERR-BODY-INVALID-INT %s
10+
11+
# Test that we successfully parse valid DIExpression metadata in MIR, and that
12+
# we reject invalid DIExpression metadata in MIR.
13+
14+
--- |
15+
define void @func() #0 !dbg !6 {
16+
entry:
17+
call void @llvm.dbg.value(metadata i32 0, metadata !10, metadata !DIExpression()), !dbg !12
18+
ret void
19+
}
20+
21+
declare void @llvm.dbg.value(metadata, metadata, metadata) #1
22+
23+
attributes #0 = { noinline nounwind optnone }
24+
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
25+
26+
!llvm.module.flags = !{!0, !1}
27+
!llvm.ident = !{!2}
28+
!llvm.dbg.cu = !{!3}
29+
!llvm.debugify = !{!5, !5}
30+
31+
!0 = !{i32 1, !"wchar_size", i32 4}
32+
!1 = !{i32 2, !"Debug Info Version", i32 3}
33+
!2 = !{!"clang"}
34+
!3 = distinct !DICompileUnit(language: DW_LANG_C, file: !4, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
35+
!4 = !DIFile(filename: "<stdin>", directory: "/")
36+
!5 = !{i32 1}
37+
!6 = distinct !DISubprogram(name: "func", linkageName: "func", scope: null, file: !4, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !3, retainedNodes: !9)
38+
!7 = !DISubroutineType(types: !8)
39+
!8 = !{}
40+
!9 = !{!10}
41+
!10 = !DILocalVariable(name: "1", scope: !6, file: !4, line: 1, type: !11)
42+
!11 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_unsigned)
43+
!12 = !DILocation(line: 1, column: 1, scope: !6)
44+
45+
...
46+
---
47+
48+
# EXPR-NONE: debug-info-expression: '!DIExpression()'
49+
# EXPR-NONE: DBG_VALUE 0, 0, ![[#]], !DIExpression(), debug-location ![[#]]
50+
51+
# EXPR-SOME: debug-info-expression: '!DIExpression(DW_OP_LLVM_arg, 0, DW_OP_dup, DW_OP_plus)'
52+
# EXPR-SOME: DBG_VALUE 0, 0, ![[#]], !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_dup, DW_OP_plus), debug-location ![[#]]
53+
54+
name: func
55+
stack:
56+
# EXPR-ERR-STACK-INVALID-OP: <stdin>:[[#@LINE+4]]:45: invalid DWARF op 'DW_OP_foobar'
57+
# EXPR-ERR-STACK-INVALID-ATTR: <stdin>:[[#@LINE+3]]:45: invalid DWARF attribute encoding 'DW_ATE_foobar'
58+
# EXPR-ERR-STACK-INVALID-INT: <stdin>:[[#@LINE+2]]:45: expected unsigned integer
59+
- { id: 0, size: 4, debug-info-variable: '!10', debug-info-location: '!12',
60+
debug-info-expression: '!DIExpression(STACK_EXPR)' }
61+
body: |
62+
bb.0.entry:
63+
; EXPR-ERR-BODY-INVALID-OP: <stdin>:[[#@LINE+3]]:40: invalid DWARF op 'DW_OP_foobar'
64+
; EXPR-ERR-BODY-INVALID-ATTR: <stdin>:[[#@LINE+2]]:40: invalid DWARF attribute encoding 'DW_ATE_foobar'
65+
; EXPR-ERR-BODY-INVALID-INT: <stdin>:[[#@LINE+1]]:40: expected unsigned integer
66+
DBG_VALUE 0, 0, !10, !DIExpression(BODY_EXPR), debug-location !12
67+
...

llvm/unittests/AsmParser/AsmParserTest.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llvm/AsmParser/SlotMapping.h"
1212
#include "llvm/IR/Constants.h"
1313
#include "llvm/IR/DataLayout.h"
14+
#include "llvm/IR/DebugInfoMetadata.h"
1415
#include "llvm/IR/LLVMContext.h"
1516
#include "llvm/IR/Module.h"
1617
#include "llvm/Support/Error.h"
@@ -411,4 +412,51 @@ TEST(AsmParserTest, InvalidDataLayoutStringCallback) {
411412
EXPECT_EQ(Mod2->getDataLayout(), FixedDL);
412413
}
413414

415+
TEST(AsmParserTest, DIExpressionBodyAtBeginningWithSlotMappingParsing) {
416+
LLVMContext Ctx;
417+
SMDiagnostic Error;
418+
StringRef Source = "";
419+
SlotMapping Mapping;
420+
auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
421+
ASSERT_TRUE(Mod != nullptr);
422+
auto &M = *Mod;
423+
unsigned Read;
424+
425+
ASSERT_EQ(Mapping.MetadataNodes.size(), 0u);
426+
427+
DIExpression *Expr;
428+
429+
Expr = parseDIExpressionBodyAtBeginning("()", Read, Error, M, &Mapping);
430+
ASSERT_TRUE(Expr);
431+
ASSERT_EQ(Expr->getNumElements(), 0u);
432+
433+
Expr = parseDIExpressionBodyAtBeginning("(0)", Read, Error, M, &Mapping);
434+
ASSERT_TRUE(Expr);
435+
ASSERT_EQ(Expr->getNumElements(), 1u);
436+
437+
Expr = parseDIExpressionBodyAtBeginning("(DW_OP_LLVM_fragment, 0, 1)", Read,
438+
Error, M, &Mapping);
439+
ASSERT_TRUE(Expr);
440+
ASSERT_EQ(Expr->getNumElements(), 3u);
441+
442+
Expr = parseDIExpressionBodyAtBeginning(
443+
"(DW_OP_LLVM_fragment, 0, 1) trailing source", Read, Error, M, &Mapping);
444+
ASSERT_TRUE(Expr);
445+
ASSERT_EQ(Expr->getNumElements(), 3u);
446+
ASSERT_EQ(Read, StringRef("(DW_OP_LLVM_fragment, 0, 1) ").size());
447+
448+
Error = {};
449+
Expr = parseDIExpressionBodyAtBeginning("i32", Read, Error, M, &Mapping);
450+
ASSERT_FALSE(Expr);
451+
ASSERT_EQ(Error.getMessage(), "expected '(' here");
452+
453+
Error = {};
454+
Expr = parseDIExpressionBodyAtBeginning(
455+
"!DIExpression(DW_OP_LLVM_fragment, 0, 1)", Read, Error, M, &Mapping);
456+
ASSERT_FALSE(Expr);
457+
ASSERT_EQ(Error.getMessage(), "expected '(' here");
458+
459+
ASSERT_EQ(Mapping.MetadataNodes.size(), 0u);
460+
}
461+
414462
} // end anonymous namespace

0 commit comments

Comments
 (0)