Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit aa50fa7

Browse files
committed
DI: Rewrite the DIBuilder local variable API
Replace the general `createLocalVariable()` with two more specific functions: `createParameterVariable()` and `createAutoVariable()`, and rewrite the documentation. Besides cleaning up the API, this avoids exposing the fake DWARF tags `DW_TAG_arg_variable` and `DW_TAG_auto_variable` to frontends, and is preparation for removing them completely. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@243764 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 3f2cb5c commit aa50fa7

File tree

6 files changed

+67
-34
lines changed

6 files changed

+67
-34
lines changed

bindings/go/llvm/DIBuilderBindings.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,18 @@ LLVMMetadataRef LLVMDIBuilderCreateFunction(
8484
}
8585

8686
LLVMMetadataRef LLVMDIBuilderCreateLocalVariable(
87-
LLVMDIBuilderRef Dref, unsigned Tag, LLVMMetadataRef Scope,
87+
LLVMDIBuilderRef Dref, unsigned, LLVMMetadataRef Scope,
8888
const char *Name, LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty,
8989
int AlwaysPreserve, unsigned Flags, unsigned ArgNo) {
9090
DIBuilder *D = unwrap(Dref);
91-
return wrap(D->createLocalVariable(
92-
Tag, unwrap<DIScope>(Scope), Name, unwrap<DIFile>(File), Line,
93-
unwrap<DIType>(Ty), AlwaysPreserve, Flags, ArgNo));
91+
// FIXME: Update the Go bindings to match the DIBuilder API.
92+
if (ArgNo)
93+
return wrap(D->createParameterVariable(
94+
unwrap<DIScope>(Scope), Name, ArgNo, unwrap<DIFile>(File), Line,
95+
unwrap<DIType>(Ty), AlwaysPreserve, Flags));
96+
return wrap(D->createAutoVariable(unwrap<DIScope>(Scope), Name,
97+
unwrap<DIFile>(File), Line,
98+
unwrap<DIType>(Ty), AlwaysPreserve, Flags));
9499
}
95100

96101
LLVMMetadataRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref,

docs/tutorial/LangImpl8.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,8 @@ argument allocas in ``PrototypeAST::CreateArgumentAllocas``.
395395
DIScope *Scope = KSDbgInfo.LexicalBlocks.back();
396396
DIFile *Unit = DBuilder->createFile(KSDbgInfo.TheCU.getFilename(),
397397
KSDbgInfo.TheCU.getDirectory());
398-
DILocalVariable D = DBuilder->createLocalVariable(
399-
dwarf::DW_TAG_arg_variable, Scope, Args[Idx], Unit, Line,
400-
KSDbgInfo.getDoubleTy(), true, 0, Idx + 1);
398+
DILocalVariable D = DBuilder->createParameterVariable(
399+
Scope, Args[Idx], Idx + 1, Unit, Line, KSDbgInfo.getDoubleTy(), true);
401400
402401
Instruction *Call = DBuilder->insertDeclare(
403402
Alloca, D, DBuilder->createExpression(), Builder.GetInsertBlock());

examples/Kaleidoscope/Chapter8/toy.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,9 +1253,8 @@ void PrototypeAST::CreateArgumentAllocas(Function *F) {
12531253
DIScope *Scope = KSDbgInfo.LexicalBlocks.back();
12541254
DIFile *Unit = DBuilder->createFile(KSDbgInfo.TheCU->getFilename(),
12551255
KSDbgInfo.TheCU->getDirectory());
1256-
DILocalVariable *D = DBuilder->createLocalVariable(
1257-
dwarf::DW_TAG_arg_variable, Scope, Args[Idx], Unit, Line,
1258-
KSDbgInfo.getDoubleTy(), true, 0, Idx + 1);
1256+
DILocalVariable *D = DBuilder->createParameterVariable(
1257+
Scope, Args[Idx], Idx + 1, Unit, Line, KSDbgInfo.getDoubleTy(), true);
12591258

12601259
DBuilder->insertDeclare(Alloca, D, DBuilder->createExpression(),
12611260
DebugLoc::get(Line, 0, Scope),

include/llvm/IR/DIBuilder.h

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -457,26 +457,36 @@ namespace llvm {
457457
unsigned LineNo, DIType *Ty, bool isLocalToUnit, llvm::Constant *Val,
458458
MDNode *Decl = nullptr);
459459

460-
/// Create a new descriptor for the specified
461-
/// local variable.
462-
/// \param Tag Dwarf TAG. Usually DW_TAG_auto_variable or
463-
/// DW_TAG_arg_variable.
464-
/// \param Scope Variable scope.
465-
/// \param Name Variable name.
466-
/// \param File File where this variable is defined.
467-
/// \param LineNo Line number.
468-
/// \param Ty Variable Type
469-
/// \param AlwaysPreserve Boolean. Set to true if debug info for this
470-
/// variable should be preserved in optimized build.
471-
/// \param Flags Flags, e.g. artificial variable.
472-
/// \param ArgNo If this variable is an argument then this argument's
473-
/// number. 1 indicates 1st argument.
474-
DILocalVariable *createLocalVariable(unsigned Tag, DIScope *Scope,
475-
StringRef Name, DIFile *File,
476-
unsigned LineNo, DIType *Ty,
460+
/// Create a new descriptor for an auto variable. This is a local variable
461+
/// that is not a subprogram parameter.
462+
///
463+
/// \c Scope must be a \a DILocalScope, and thus its scope chain eventually
464+
/// leads to a \a DISubprogram.
465+
///
466+
/// If \c AlwaysPreserve, this variable will be referenced from its
467+
/// containing subprogram, and will survive some optimizations.
468+
DILocalVariable *createAutoVariable(DIScope *Scope, StringRef Name,
469+
DIFile *File, unsigned LineNo,
470+
DIType *Ty,
477471
bool AlwaysPreserve = false,
478-
unsigned Flags = 0,
479-
unsigned ArgNo = 0);
472+
unsigned Flags = 0);
473+
474+
/// Create a new descriptor for a parameter variable.
475+
///
476+
/// \c Scope must be a \a DILocalScope, and thus its scope chain eventually
477+
/// leads to a \a DISubprogram.
478+
///
479+
/// \c ArgNo is the index (starting from \c 1) of this variable in the
480+
/// subprogram parameters. \c ArgNo should not conflict with other
481+
/// parameters of the same subprogram.
482+
///
483+
/// If \c AlwaysPreserve, this variable will be referenced from its
484+
/// containing subprogram, and will survive some optimizations.
485+
DILocalVariable *createParameterVariable(DIScope *Scope, StringRef Name,
486+
unsigned ArgNo, DIFile *File,
487+
unsigned LineNo, DIType *Ty,
488+
bool AlwaysPreserve = false,
489+
unsigned Flags = 0);
480490

481491
/// Create a new descriptor for the specified
482492
/// variable which has a complex address expression for its address.

lib/IR/DIBuilder.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,15 +602,18 @@ DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl(
602602
.release();
603603
}
604604

605-
DILocalVariable *DIBuilder::createLocalVariable(
606-
unsigned Tag, DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNo,
607-
DIType *Ty, bool AlwaysPreserve, unsigned Flags, unsigned ArgNo) {
605+
static DILocalVariable *createLocalVariable(
606+
LLVMContext &VMContext,
607+
DenseMap<MDNode *, std::vector<TrackingMDNodeRef>> &PreservedVariables,
608+
DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
609+
unsigned LineNo, DIType *Ty, bool AlwaysPreserve, unsigned Flags) {
608610
// FIXME: Why getNonCompileUnitScope()?
609611
// FIXME: Why is "!Context" okay here?
610612
// FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT
611613
// the only valid scopes)?
612614
DIScope *Context = getNonCompileUnitScope(Scope);
613615

616+
dwarf::Tag Tag = ArgNo ? dwarf::DW_TAG_arg_variable : dwarf::DW_TAG_auto_variable;
614617
auto *Node = DILocalVariable::get(
615618
VMContext, Tag, cast_or_null<DILocalScope>(Context), Name, File, LineNo,
616619
DITypeRef::get(Ty), ArgNo, Flags);
@@ -625,6 +628,23 @@ DILocalVariable *DIBuilder::createLocalVariable(
625628
return Node;
626629
}
627630

631+
DILocalVariable *DIBuilder::createAutoVariable(DIScope *Scope, StringRef Name,
632+
DIFile *File, unsigned LineNo,
633+
DIType *Ty, bool AlwaysPreserve,
634+
unsigned Flags) {
635+
return createLocalVariable(VMContext, PreservedVariables, Scope, Name,
636+
/* ArgNo */ 0, File, LineNo, Ty, AlwaysPreserve,
637+
Flags);
638+
}
639+
640+
DILocalVariable *DIBuilder::createParameterVariable(
641+
DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
642+
unsigned LineNo, DIType *Ty, bool AlwaysPreserve, unsigned Flags) {
643+
assert(ArgNo && "Expected non-zero argument number for parameter");
644+
return createLocalVariable(VMContext, PreservedVariables, Scope, Name, ArgNo,
645+
File, LineNo, Ty, AlwaysPreserve, Flags);
646+
}
647+
628648
DIExpression *DIBuilder::createExpression(ArrayRef<uint64_t> Addr) {
629649
return DIExpression::get(VMContext, Addr);
630650
}

unittests/Transforms/Utils/Cloning.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ class CloneFunc : public ::testing::Test {
255255
auto *IntType =
256256
DBuilder.createBasicType("int", 32, 0, dwarf::DW_ATE_signed);
257257
auto *E = DBuilder.createExpression();
258-
auto *Variable = DBuilder.createLocalVariable(
259-
dwarf::DW_TAG_auto_variable, Subprogram, "x", File, 5, IntType, true);
258+
auto *Variable =
259+
DBuilder.createAutoVariable(Subprogram, "x", File, 5, IntType, true);
260260
auto *DL = DILocation::get(Subprogram->getContext(), 5, 0, Subprogram);
261261
DBuilder.insertDeclare(Alloca, Variable, E, DL, Store);
262262
DBuilder.insertDbgValueIntrinsic(AllocaContent, 0, Variable, E, DL,

0 commit comments

Comments
 (0)