Skip to content

Commit 969129f

Browse files
committed
[SYCL] Refactor ThreeTransform in SemaSYCL.cpp
We use this ThreeTransform to change only one object to another. No need to use DenseMap here, std::pair is enough. Signed-off-by: Mariya Podchishchaeva <[email protected]>
1 parent 405174c commit 969129f

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
using namespace clang;
2828

29-
typedef llvm::DenseMap<DeclaratorDecl *, DeclaratorDecl *> DeclMap;
30-
3129
using KernelParamKind = SYCLIntegrationHeader::kernel_param_kind_t;
3230

3331
enum target {
@@ -376,27 +374,25 @@ class MarkDeviceFunction : public RecursiveASTVisitor<MarkDeviceFunction> {
376374

377375
class KernelBodyTransform : public TreeTransform<KernelBodyTransform> {
378376
public:
379-
KernelBodyTransform(llvm::DenseMap<DeclaratorDecl *, DeclaratorDecl *> &Map,
377+
KernelBodyTransform(std::pair<DeclaratorDecl *, DeclaratorDecl *> &MPair,
380378
Sema &S)
381-
: TreeTransform<KernelBodyTransform>(S), DMap(Map), SemaRef(S) {}
379+
: TreeTransform<KernelBodyTransform>(S), MappingPair(MPair), SemaRef(S) {}
382380
bool AlwaysRebuild() { return true; }
383381

384382
ExprResult TransformDeclRefExpr(DeclRefExpr *DRE) {
385383
auto Ref = dyn_cast<DeclaratorDecl>(DRE->getDecl());
386-
if (Ref) {
387-
auto NewDecl = DMap[Ref];
388-
if (NewDecl) {
389-
return DeclRefExpr::Create(
390-
SemaRef.getASTContext(), DRE->getQualifierLoc(),
391-
DRE->getTemplateKeywordLoc(), NewDecl, false, DRE->getNameInfo(),
392-
NewDecl->getType(), DRE->getValueKind());
393-
}
384+
if (Ref && Ref == MappingPair.first) {
385+
auto NewDecl = MappingPair.second;
386+
return DeclRefExpr::Create(
387+
SemaRef.getASTContext(), DRE->getQualifierLoc(),
388+
DRE->getTemplateKeywordLoc(), NewDecl, false, DRE->getNameInfo(),
389+
NewDecl->getType(), DRE->getValueKind());
394390
}
395391
return DRE;
396392
}
397393

398394
private:
399-
DeclMap DMap;
395+
std::pair<DeclaratorDecl *, DeclaratorDecl *> MappingPair;
400396
Sema &SemaRef;
401397
};
402398

@@ -618,21 +614,23 @@ CreateSYCLKernelBody(Sema &S, FunctionDecl *KernelCallerFunc, DeclContext *Kerne
618614
KernelFuncParam++;
619615
}
620616
}
621-
// In function from headers lambda is function parameter, we need
622-
// to replace all refs to this lambda with our vardecl.
623-
// I used TreeTransform here, but I'm not sure that it is good solution
624-
// Also I used map and I'm not sure about it too.
625-
// TODO SYCL review the above design concerns
617+
618+
// In kernel caller function lambda/functior is function parameter, we need
619+
// to replace all refs to this lambda/functor with our kernel object clone
620+
// declared inside kernel body.
626621
Stmt *FunctionBody = KernelCallerFunc->getBody();
627-
DeclMap DMap;
628622
ParmVarDecl *KernelObjParam = *(KernelCallerFunc->param_begin());
623+
629624
// DeclRefExpr with valid source location but with decl which is not marked
630625
// as used is invalid.
631626
KernelObjClone->setIsUsed();
632-
DMap[KernelObjParam] = KernelObjClone;
633-
// Without PushFunctionScope I had segfault. Maybe we also need to do pop.
627+
std::pair<DeclaratorDecl *, DeclaratorDecl *> MappingPair;
628+
MappingPair.first = KernelObjParam;
629+
MappingPair.second = KernelObjClone;
630+
631+
// Function scope might be empty, so we do push
634632
S.PushFunctionScope();
635-
KernelBodyTransform KBT(DMap, S);
633+
KernelBodyTransform KBT(MappingPair, S);
636634
Stmt *NewBody = KBT.TransformStmt(FunctionBody).get();
637635
BodyStmts.push_back(NewBody);
638636
return CompoundStmt::Create(S.Context, BodyStmts, SourceLocation(),

0 commit comments

Comments
 (0)