Skip to content

Commit ebdffb7

Browse files
committed
Merge from 'master' to 'sycl-web' (#1)
CONFLICT (content): Merge conflict in clang/include/clang/Basic/Attr.td
2 parents da194d3 + c97be2c commit ebdffb7

File tree

25 files changed

+383
-127
lines changed

25 files changed

+383
-127
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,13 +1061,6 @@ def CUDADevice : InheritableAttr {
10611061
let Documentation = [Undocumented];
10621062
}
10631063

1064-
def HIPPinnedShadow : InheritableAttr {
1065-
let Spellings = [GNU<"hip_pinned_shadow">, Declspec<"__hip_pinned_shadow__">];
1066-
let Subjects = SubjectList<[Var]>;
1067-
let LangOpts = [HIP];
1068-
let Documentation = [HIPPinnedShadowDocs];
1069-
}
1070-
10711064
def CUDADeviceBuiltin : IgnoredAttr {
10721065
let Spellings = [GNU<"device_builtin">, Declspec<"__device_builtin__">];
10731066
let LangOpts = [CUDA];

clang/include/clang/Basic/AttrDocs.td

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5052,18 +5052,6 @@ only call one function.
50525052
}];
50535053
}
50545054

5055-
def HIPPinnedShadowDocs : Documentation {
5056-
let Category = DocCatType;
5057-
let Content = [{
5058-
The GNU style attribute __attribute__((hip_pinned_shadow)) or MSVC style attribute
5059-
__declspec(hip_pinned_shadow) can be added to the definition of a global variable
5060-
to indicate it is a HIP pinned shadow variable. A HIP pinned shadow variable can
5061-
be accessed on both device side and host side. It has external linkage and is
5062-
not initialized on device side. It has internal linkage and is initialized by
5063-
the initializer on host side.
5064-
}];
5065-
}
5066-
50675055
def CUDADeviceBuiltinSurfaceTypeDocs : Documentation {
50685056
let Category = DocCatType;
50695057
let Content = [{

clang/include/clang/Tooling/Syntax/Tokens.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Token &T);
171171
/// To build a token buffer use the TokenCollector class. You can also compute
172172
/// the spelled tokens of a file using the tokenize() helper.
173173
///
174-
/// FIXME: allow to map from spelled to expanded tokens when use-case shows up.
175174
/// FIXME: allow mappings into macro arguments.
176175
class TokenBuffer {
177176
public:
@@ -228,6 +227,36 @@ class TokenBuffer {
228227
llvm::Optional<llvm::ArrayRef<syntax::Token>>
229228
spelledForExpanded(llvm::ArrayRef<syntax::Token> Expanded) const;
230229

230+
/// Find the subranges of expanded tokens, corresponding to \p Spelled.
231+
///
232+
/// Some spelled tokens may not be present in the expanded token stream, so
233+
/// this function can return an empty vector, e.g. for tokens of macro
234+
/// directives or disabled preprocessor branches.
235+
///
236+
/// Some spelled tokens can be duplicated in the expanded token stream
237+
/// multiple times and this function will return multiple results in those
238+
/// cases. This happens when \p Spelled is inside a macro argument.
239+
///
240+
/// FIXME: return correct results on macro arguments. For now, we return an
241+
/// empty list.
242+
///
243+
/// (!) will return empty vector on tokens from #define body:
244+
/// E.g. for the following example:
245+
///
246+
/// #define FIRST(A) f1 A = A f2
247+
/// #define SECOND s
248+
///
249+
/// a FIRST(arg) b SECOND c // expanded tokens are: a f1 arg = arg f2 b s
250+
/// The results would be
251+
/// spelled => expanded
252+
/// ------------------------
253+
/// #define FIRST => {}
254+
/// a FIRST(arg) => {a f1 arg = arg f2}
255+
/// arg => {arg, arg} // arg #1 is before `=` and arg #2 is
256+
/// // after `=` in the expanded tokens.
257+
llvm::SmallVector<llvm::ArrayRef<syntax::Token>, 1>
258+
expandedForSpelled(llvm::ArrayRef<syntax::Token> Spelled) const;
259+
231260
/// An expansion produced by the preprocessor, includes macro expansions and
232261
/// preprocessor directives. Preprocessor always maps a non-empty range of
233262
/// spelled tokens to a (possibly empty) range of expanded tokens. Here is a
@@ -317,6 +346,12 @@ class TokenBuffer {
317346
std::pair<const syntax::Token *, const Mapping *>
318347
spelledForExpandedToken(const syntax::Token *Expanded) const;
319348

349+
/// Returns a mapping starting before \p Spelled token, or nullptr if no
350+
/// such mapping exists.
351+
static const Mapping *
352+
mappingStartingBeforeSpelled(const MarkedFile &F,
353+
const syntax::Token *Spelled);
354+
320355
/// Token stream produced after preprocessing, conceputally this captures the
321356
/// same stream as 'clang -E' (excluding the preprocessor directives like
322357
/// #file, etc.).

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,9 +1990,9 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
19901990
}
19911991
}
19921992

1993-
void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV, bool SkipCheck) {
1994-
assert(SkipCheck || (!GV->isDeclaration() &&
1995-
"Only globals with definition can force usage."));
1993+
void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
1994+
assert(!GV->isDeclaration() &&
1995+
"Only globals with definition can force usage.");
19961996
LLVMUsed.emplace_back(GV);
19971997
}
19981998

@@ -2555,7 +2555,6 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) {
25552555
!Global->hasAttr<CUDAGlobalAttr>() &&
25562556
!Global->hasAttr<CUDAConstantAttr>() &&
25572557
!Global->hasAttr<CUDASharedAttr>() &&
2558-
!(LangOpts.HIP && Global->hasAttr<HIPPinnedShadowAttr>()) &&
25592558
!Global->getType()->isCUDADeviceBuiltinSurfaceType() &&
25602559
!Global->getType()->isCUDADeviceBuiltinTextureType())
25612560
return;
@@ -4122,10 +4121,8 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
41224121
D->getType()->isCUDADeviceBuiltinTextureType());
41234122
// HIP pinned shadow of initialized host-side global variables are also
41244123
// left undefined.
4125-
bool IsHIPPinnedShadowVar =
4126-
getLangOpts().CUDAIsDevice && D->hasAttr<HIPPinnedShadowAttr>();
4127-
if (getLangOpts().CUDA && (IsCUDASharedVar || IsCUDAShadowVar ||
4128-
IsCUDADeviceShadowVar || IsHIPPinnedShadowVar))
4124+
if (getLangOpts().CUDA &&
4125+
(IsCUDASharedVar || IsCUDAShadowVar || IsCUDADeviceShadowVar))
41294126
Init = llvm::UndefValue::get(getTypes().ConvertType(ASTTy));
41304127
else if (D->hasAttr<LoaderUninitializedAttr>())
41314128
Init = llvm::UndefValue::get(getTypes().ConvertType(ASTTy));
@@ -4248,8 +4245,7 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
42484245
// global variables become internal definitions. These have to
42494246
// be internal in order to prevent name conflicts with global
42504247
// host variables with the same name in a different TUs.
4251-
if (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
4252-
D->hasAttr<HIPPinnedShadowAttr>()) {
4248+
if (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>()) {
42534249
Linkage = llvm::GlobalValue::InternalLinkage;
42544250
// Shadow variables and their properties must be registered with CUDA
42554251
// runtime. Skip Extern global variables, which will be registered in
@@ -4296,15 +4292,7 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
42964292
}
42974293
}
42984294

4299-
// HIPPinnedShadowVar should remain in the final code object irrespective of
4300-
// whether it is used or not within the code. Add it to used list, so that
4301-
// it will not get eliminated when it is unused. Also, it is an extern var
4302-
// within device code, and it should *not* get initialized within device code.
4303-
if (IsHIPPinnedShadowVar)
4304-
addUsedGlobal(GV, /*SkipCheck=*/true);
4305-
else
4306-
GV->setInitializer(Init);
4307-
4295+
GV->setInitializer(Init);
43084296
if (emitter)
43094297
emitter->finalize(GV);
43104298

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ class CodeGenModule : public CodeGenTypeCache {
10501050
void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
10511051

10521052
/// Add a global to a list to be added to the llvm.used metadata.
1053-
void addUsedGlobal(llvm::GlobalValue *GV, bool SkipCheck = false);
1053+
void addUsedGlobal(llvm::GlobalValue *GV);
10541054

10551055
/// Add a global to a list to be added to the llvm.compiler.used metadata.
10561056
void addCompilerUsedGlobal(llvm::GlobalValue *GV);

clang/lib/CodeGen/TargetInfo.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8407,23 +8407,13 @@ static bool requiresAMDGPUProtectedVisibility(const Decl *D,
84078407
(isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
84088408
(isa<VarDecl>(D) &&
84098409
(D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
8410-
D->hasAttr<HIPPinnedShadowAttr>()));
8411-
}
8412-
8413-
static bool requiresAMDGPUDefaultVisibility(const Decl *D,
8414-
llvm::GlobalValue *GV) {
8415-
if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
8416-
return false;
8417-
8418-
return isa<VarDecl>(D) && D->hasAttr<HIPPinnedShadowAttr>();
8410+
cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() ||
8411+
cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType()));
84198412
}
84208413

84218414
void AMDGPUTargetCodeGenInfo::setTargetAttributes(
84228415
const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
8423-
if (requiresAMDGPUDefaultVisibility(D, GV)) {
8424-
GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
8425-
GV->setDSOLocal(false);
8426-
} else if (requiresAMDGPUProtectedVisibility(D, GV)) {
8416+
if (requiresAMDGPUProtectedVisibility(D, GV)) {
84278417
GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
84288418
GV->setDSOLocal(true);
84298419
}

clang/lib/Driver/ToolChains/HIP.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
192192
const char *InputFileName) const {
193193
// Construct lld command.
194194
// The output from ld.lld is an HSA code object file.
195-
ArgStringList LldArgs{
196-
"-flavor", "gnu", "-shared", "-o", Output.getFilename(), InputFileName};
195+
ArgStringList LldArgs{"-flavor", "gnu", "--no-undefined",
196+
"-shared", "-o", Output.getFilename(),
197+
InputFileName};
197198
const char *Lld = Args.MakeArgString(getToolChain().GetProgramPath("lld"));
198199
C.addCommand(std::make_unique<Command>(JA, *this, Lld, LldArgs, Inputs));
199200
}

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7519,10 +7519,6 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
75197519
case ParsedAttr::AT_CUDAHost:
75207520
handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, AL);
75217521
break;
7522-
case ParsedAttr::AT_HIPPinnedShadow:
7523-
handleSimpleAttributeWithExclusions<HIPPinnedShadowAttr, CUDADeviceAttr,
7524-
CUDAConstantAttr>(S, D, AL);
7525-
break;
75267522
case ParsedAttr::AT_CUDADeviceBuiltinSurfaceType:
75277523
handleSimpleAttributeWithExclusions<CUDADeviceBuiltinSurfaceTypeAttr,
75287524
CUDADeviceBuiltinTextureTypeAttr>(S, D,

clang/lib/Tooling/Syntax/Tokens.cpp

Lines changed: 136 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,19 +213,109 @@ TokenBuffer::spelledForExpandedToken(const syntax::Token *Expanded) const {
213213
// Our token could only be produced by the previous mapping.
214214
if (It == File.Mappings.begin()) {
215215
// No previous mapping, no need to modify offsets.
216-
return {&File.SpelledTokens[ExpandedIndex - File.BeginExpanded], nullptr};
216+
return {&File.SpelledTokens[ExpandedIndex - File.BeginExpanded],
217+
/*Mapping=*/nullptr};
217218
}
218219
--It; // 'It' now points to last mapping that started before our token.
219220

220221
// Check if the token is part of the mapping.
221222
if (ExpandedIndex < It->EndExpanded)
222-
return {&File.SpelledTokens[It->BeginSpelled], /*Mapping*/ &*It};
223+
return {&File.SpelledTokens[It->BeginSpelled], /*Mapping=*/&*It};
223224

224225
// Not part of the mapping, use the index from previous mapping to compute the
225226
// corresponding spelled token.
226227
return {
227228
&File.SpelledTokens[It->EndSpelled + (ExpandedIndex - It->EndExpanded)],
228-
/*Mapping*/ nullptr};
229+
/*Mapping=*/nullptr};
230+
}
231+
232+
const TokenBuffer::Mapping *
233+
TokenBuffer::mappingStartingBeforeSpelled(const MarkedFile &F,
234+
const syntax::Token *Spelled) {
235+
assert(F.SpelledTokens.data() <= Spelled);
236+
unsigned SpelledI = Spelled - F.SpelledTokens.data();
237+
assert(SpelledI < F.SpelledTokens.size());
238+
239+
auto It = llvm::partition_point(F.Mappings, [SpelledI](const Mapping &M) {
240+
return M.BeginSpelled <= SpelledI;
241+
});
242+
if (It == F.Mappings.begin())
243+
return nullptr;
244+
--It;
245+
return &*It;
246+
}
247+
248+
llvm::SmallVector<llvm::ArrayRef<syntax::Token>, 1>
249+
TokenBuffer::expandedForSpelled(llvm::ArrayRef<syntax::Token> Spelled) const {
250+
if (Spelled.empty())
251+
return {};
252+
assert(Spelled.front().location().isFileID());
253+
254+
auto FID = sourceManager().getFileID(Spelled.front().location());
255+
auto It = Files.find(FID);
256+
assert(It != Files.end());
257+
258+
const MarkedFile &File = It->second;
259+
// `Spelled` must be a subrange of `File.SpelledTokens`.
260+
assert(File.SpelledTokens.data() <= Spelled.data());
261+
assert(&Spelled.back() <=
262+
File.SpelledTokens.data() + File.SpelledTokens.size());
263+
#ifndef NDEBUG
264+
auto T1 = Spelled.back().location();
265+
auto T2 = File.SpelledTokens.back().location();
266+
assert(T1 == T2 || sourceManager().isBeforeInTranslationUnit(T1, T2));
267+
#endif
268+
269+
auto *FrontMapping = mappingStartingBeforeSpelled(File, &Spelled.front());
270+
unsigned SpelledFrontI = &Spelled.front() - File.SpelledTokens.data();
271+
assert(SpelledFrontI < File.SpelledTokens.size());
272+
unsigned ExpandedBegin;
273+
if (!FrontMapping) {
274+
// No mapping that starts before the first token of Spelled, we don't have
275+
// to modify offsets.
276+
ExpandedBegin = File.BeginExpanded + SpelledFrontI;
277+
} else if (SpelledFrontI < FrontMapping->EndSpelled) {
278+
// This mapping applies to Spelled tokens.
279+
if (SpelledFrontI != FrontMapping->BeginSpelled) {
280+
// Spelled tokens don't cover the entire mapping, returning empty result.
281+
return {}; // FIXME: support macro arguments.
282+
}
283+
// Spelled tokens start at the beginning of this mapping.
284+
ExpandedBegin = FrontMapping->BeginExpanded;
285+
} else {
286+
// Spelled tokens start after the mapping ends (they start in the hole
287+
// between 2 mappings, or between a mapping and end of the file).
288+
ExpandedBegin =
289+
FrontMapping->EndExpanded + (SpelledFrontI - FrontMapping->EndSpelled);
290+
}
291+
292+
auto *BackMapping = mappingStartingBeforeSpelled(File, &Spelled.back());
293+
unsigned SpelledBackI = &Spelled.back() - File.SpelledTokens.data();
294+
unsigned ExpandedEnd;
295+
if (!BackMapping) {
296+
// No mapping that starts before the last token of Spelled, we don't have to
297+
// modify offsets.
298+
ExpandedEnd = File.BeginExpanded + SpelledBackI + 1;
299+
} else if (SpelledBackI < BackMapping->EndSpelled) {
300+
// This mapping applies to Spelled tokens.
301+
if (SpelledBackI + 1 != BackMapping->EndSpelled) {
302+
// Spelled tokens don't cover the entire mapping, returning empty result.
303+
return {}; // FIXME: support macro arguments.
304+
}
305+
ExpandedEnd = BackMapping->EndExpanded;
306+
} else {
307+
// Spelled tokens end after the mapping ends.
308+
ExpandedEnd =
309+
BackMapping->EndExpanded + (SpelledBackI - BackMapping->EndSpelled) + 1;
310+
}
311+
312+
assert(ExpandedBegin < ExpandedTokens.size());
313+
assert(ExpandedEnd < ExpandedTokens.size());
314+
// Avoid returning empty ranges.
315+
if (ExpandedBegin == ExpandedEnd)
316+
return {};
317+
return {llvm::makeArrayRef(ExpandedTokens.data() + ExpandedBegin,
318+
ExpandedTokens.data() + ExpandedEnd)};
229319
}
230320

231321
llvm::ArrayRef<syntax::Token> TokenBuffer::spelledTokens(FileID FID) const {
@@ -436,14 +526,38 @@ class TokenCollector::CollectPPExpansions : public PPCallbacks {
436526
SourceRange Range, const MacroArgs *Args) override {
437527
if (!Collector)
438528
return;
439-
// Only record top-level expansions, not those where:
529+
const auto &SM = Collector->PP.getSourceManager();
530+
// Only record top-level expansions that directly produce expanded tokens.
531+
// This excludes those where:
440532
// - the macro use is inside a macro body,
441533
// - the macro appears in an argument to another macro.
442-
if (!MacroNameTok.getLocation().isFileID() ||
443-
(LastExpansionEnd.isValid() &&
444-
Collector->PP.getSourceManager().isBeforeInTranslationUnit(
445-
Range.getBegin(), LastExpansionEnd)))
534+
// However macro expansion isn't really a tree, it's token rewrite rules,
535+
// so there are other cases, e.g.
536+
// #define B(X) X
537+
// #define A 1 + B
538+
// A(2)
539+
// Both A and B produce expanded tokens, though the macro name 'B' comes
540+
// from an expansion. The best we can do is merge the mappings for both.
541+
542+
// The *last* token of any top-level macro expansion must be in a file.
543+
// (In the example above, see the closing paren of the expansion of B).
544+
if (!Range.getEnd().isFileID())
545+
return;
546+
// If there's a current expansion that encloses this one, this one can't be
547+
// top-level.
548+
if (LastExpansionEnd.isValid() &&
549+
!SM.isBeforeInTranslationUnit(LastExpansionEnd, Range.getEnd()))
446550
return;
551+
552+
// If the macro invocation (B) starts in a macro (A) but ends in a file,
553+
// we'll create a merged mapping for A + B by overwriting the endpoint for
554+
// A's startpoint.
555+
if (!Range.getBegin().isFileID()) {
556+
Range.setBegin(SM.getExpansionLoc(Range.getBegin()));
557+
assert(Collector->Expansions.count(Range.getBegin().getRawEncoding()) &&
558+
"Overlapping macros should have same expansion location");
559+
}
560+
447561
Collector->Expansions[Range.getBegin().getRawEncoding()] = Range.getEnd();
448562
LastExpansionEnd = Range.getEnd();
449563
}
@@ -526,6 +640,20 @@ class TokenCollector::Builder {
526640
for (const auto &File : Result.Files)
527641
discard(File.first);
528642

643+
#ifndef NDEBUG
644+
for (auto &pair : Result.Files) {
645+
auto &mappings = pair.second.Mappings;
646+
assert(std::is_sorted(
647+
mappings.begin(), mappings.end(),
648+
[](const TokenBuffer::Mapping &M1, const TokenBuffer::Mapping &M2) {
649+
return M1.BeginSpelled < M2.BeginSpelled &&
650+
M1.EndSpelled < M2.EndSpelled &&
651+
M1.BeginExpanded < M2.BeginExpanded &&
652+
M1.EndExpanded < M2.EndExpanded;
653+
}));
654+
}
655+
#endif
656+
529657
return std::move(Result);
530658
}
531659

0 commit comments

Comments
 (0)