Skip to content

Commit ec6b5ab

Browse files
authored
Merge branch 'main' into harrison/mlir
2 parents 3f9f7b0 + 6b64f36 commit ec6b5ab

File tree

112 files changed

+1143
-525
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+1143
-525
lines changed

clang-tools-extra/clang-query/Query.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ bool HelpQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
4444
" set bind-root (true|false) "
4545
"Set whether to bind the root matcher to \"root\".\n"
4646
" set print-matcher (true|false) "
47-
"Set whether to print the current matcher,\n"
47+
"Set whether to print the current matcher.\n"
48+
" set enable-profile (true|false) "
49+
"Set whether to enable matcher profiling.\n"
4850
" set traversal <kind> "
4951
"Set traversal kind of clang-query session. Available kinds are:\n"
5052
" AsIs "
@@ -82,27 +84,53 @@ namespace {
8284

8385
struct CollectBoundNodes : MatchFinder::MatchCallback {
8486
std::vector<BoundNodes> &Bindings;
85-
CollectBoundNodes(std::vector<BoundNodes> &Bindings) : Bindings(Bindings) {}
87+
StringRef Unit;
88+
CollectBoundNodes(std::vector<BoundNodes> &Bindings, StringRef Unit)
89+
: Bindings(Bindings), Unit(Unit) {}
8690
void run(const MatchFinder::MatchResult &Result) override {
8791
Bindings.push_back(Result.Nodes);
8892
}
93+
StringRef getID() const override { return Unit; }
94+
};
95+
96+
struct QueryProfiler {
97+
llvm::StringMap<llvm::TimeRecord> Records;
98+
99+
~QueryProfiler() {
100+
llvm::TimerGroup TG("clang-query", "clang-query matcher profiling",
101+
Records);
102+
TG.print(llvm::errs());
103+
llvm::errs().flush();
104+
}
89105
};
90106

91107
} // namespace
92108

93109
bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
94110
unsigned MatchCount = 0;
95111

112+
std::optional<QueryProfiler> Profiler;
113+
if (QS.EnableProfile)
114+
Profiler.emplace();
115+
96116
for (auto &AST : QS.ASTs) {
97-
MatchFinder Finder;
117+
ast_matchers::MatchFinder::MatchFinderOptions FinderOptions;
118+
std::optional<llvm::StringMap<llvm::TimeRecord>> Records;
119+
if (QS.EnableProfile) {
120+
Records.emplace();
121+
FinderOptions.CheckProfiling.emplace(*Records);
122+
}
123+
124+
MatchFinder Finder(FinderOptions);
98125
std::vector<BoundNodes> Matches;
99126
DynTypedMatcher MaybeBoundMatcher = Matcher;
100127
if (QS.BindRoot) {
101128
std::optional<DynTypedMatcher> M = Matcher.tryBind("root");
102129
if (M)
103130
MaybeBoundMatcher = *M;
104131
}
105-
CollectBoundNodes Collect(Matches);
132+
StringRef OrigSrcName = AST->getOriginalSourceFileName();
133+
CollectBoundNodes Collect(Matches, OrigSrcName);
106134
if (!Finder.addDynamicMatcher(MaybeBoundMatcher, &Collect)) {
107135
OS << "Not a valid top-level matcher.\n";
108136
return false;
@@ -111,6 +139,8 @@ bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
111139
ASTContext &Ctx = AST->getASTContext();
112140
Ctx.getParentMapContext().setTraversalKind(QS.TK);
113141
Finder.matchAST(Ctx);
142+
if (QS.EnableProfile)
143+
Profiler->Records[OrigSrcName] += (*Records)[OrigSrcName];
114144

115145
if (QS.PrintMatcher) {
116146
SmallVector<StringRef, 4> Lines;

clang-tools-extra/clang-query/QueryParser.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ enum ParsedQueryVariable {
182182
PQV_Output,
183183
PQV_BindRoot,
184184
PQV_PrintMatcher,
185+
PQV_EnableProfile,
185186
PQV_Traversal
186187
};
187188

@@ -285,6 +286,7 @@ QueryRef QueryParser::doParse() {
285286
.Case("output", PQV_Output)
286287
.Case("bind-root", PQV_BindRoot)
287288
.Case("print-matcher", PQV_PrintMatcher)
289+
.Case("enable-profile", PQV_EnableProfile)
288290
.Case("traversal", PQV_Traversal)
289291
.Default(PQV_Invalid);
290292
if (VarStr.empty())
@@ -303,6 +305,9 @@ QueryRef QueryParser::doParse() {
303305
case PQV_PrintMatcher:
304306
Q = parseSetBool(&QuerySession::PrintMatcher);
305307
break;
308+
case PQV_EnableProfile:
309+
Q = parseSetBool(&QuerySession::EnableProfile);
310+
break;
306311
case PQV_Traversal:
307312
Q = parseSetTraversalKind(&QuerySession::TK);
308313
break;

clang-tools-extra/clang-query/QuerySession.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class QuerySession {
2626
QuerySession(llvm::ArrayRef<std::unique_ptr<ASTUnit>> ASTs)
2727
: ASTs(ASTs), PrintOutput(false), DiagOutput(true),
2828
DetailedASTOutput(false), BindRoot(true), PrintMatcher(false),
29-
Terminate(false), TK(TK_AsIs) {}
29+
EnableProfile(false), Terminate(false), TK(TK_AsIs) {}
3030

3131
llvm::ArrayRef<std::unique_ptr<ASTUnit>> ASTs;
3232

@@ -36,6 +36,7 @@ class QuerySession {
3636

3737
bool BindRoot;
3838
bool PrintMatcher;
39+
bool EnableProfile;
3940
bool Terminate;
4041

4142
TraversalKind TK;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Improvements to clang-doc
9898
Improvements to clang-query
9999
---------------------------
100100

101-
The improvements are...
101+
- Added `set enable-profile true/false` command for basic matcher profiling.
102102

103103
Improvements to clang-tidy
104104
--------------------------

clang/utils/TableGen/ClangAttrEmitter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,7 +2727,8 @@ static void emitAttributes(const RecordKeeper &Records, raw_ostream &OS,
27272727
}
27282728

27292729
if (Header)
2730-
OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
2730+
OS << "class CLANG_ABI " << R.getName() << "Attr : public " << SuperName
2731+
<< " {\n";
27312732
else
27322733
OS << "\n// " << R.getName() << "Attr implementation\n\n";
27332734

@@ -3185,7 +3186,8 @@ void clang::EmitClangAttrClass(const RecordKeeper &Records, raw_ostream &OS) {
31853186
emitSourceFileHeader("Attribute classes' definitions", OS, Records);
31863187

31873188
OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
3188-
OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
3189+
OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n";
3190+
OS << "#include \"clang/Support/Compiler.h\"\n\n";
31893191

31903192
emitAttributes(Records, OS, true);
31913193

flang/lib/Optimizer/Analysis/AliasAnalysis.cpp

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,29 @@ getAttrsFromVariable(fir::FortranVariableOpInterface var) {
372372
return attrs;
373373
}
374374

375+
template <typename OMPTypeOp, typename DeclTypeOp>
376+
static Value getPrivateArg(omp::BlockArgOpenMPOpInterface &argIface,
377+
OMPTypeOp &op, DeclTypeOp &declOp) {
378+
Value privateArg;
379+
if (!op.getPrivateSyms().has_value())
380+
return privateArg;
381+
for (auto [opSym, blockArg] :
382+
llvm::zip_equal(*op.getPrivateSyms(), argIface.getPrivateBlockArgs())) {
383+
if (blockArg == declOp.getMemref()) {
384+
omp::PrivateClauseOp privateOp =
385+
SymbolTable::lookupNearestSymbolFrom<omp::PrivateClauseOp>(
386+
op, cast<SymbolRefAttr>(opSym));
387+
privateOp.walk([&](omp::YieldOp yieldOp) {
388+
llvm::TypeSwitch<Operation *>(yieldOp.getResults()[0].getDefiningOp())
389+
.template Case<fir::DeclareOp, hlfir::DeclareOp>(
390+
[&](auto declOp) { privateArg = declOp.getMemref(); });
391+
});
392+
return privateArg;
393+
}
394+
}
395+
return privateArg;
396+
}
397+
375398
AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
376399
bool getInstantiationPoint) {
377400
auto *defOp = v.getDefiningOp();
@@ -470,20 +493,37 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
470493
breakFromLoop = true;
471494
})
472495
.Case<hlfir::DeclareOp, fir::DeclareOp>([&](auto op) {
473-
// If declare operation is inside omp target region,
474-
// continue alias analysis outside the target region
475-
if (auto targetOp =
476-
llvm::dyn_cast<omp::TargetOp>(op->getParentOp())) {
477-
auto argIface = cast<omp::BlockArgOpenMPOpInterface>(*targetOp);
478-
for (auto [opArg, blockArg] : llvm::zip_equal(
479-
targetOp.getMapVars(), argIface.getMapBlockArgs())) {
480-
if (blockArg == op.getMemref()) {
481-
omp::MapInfoOp mapInfo =
482-
llvm::cast<omp::MapInfoOp>(opArg.getDefiningOp());
483-
v = mapInfo.getVarPtr();
484-
defOp = v.getDefiningOp();
485-
return;
486-
}
496+
if (omp::BlockArgOpenMPOpInterface argIface =
497+
dyn_cast<omp::BlockArgOpenMPOpInterface>(op->getParentOp())) {
498+
Value ompValArg;
499+
llvm::TypeSwitch<Operation *>(op->getParentOp())
500+
.template Case<omp::TargetOp>([&](auto targetOp) {
501+
// If declare operation is inside omp target region,
502+
// continue alias analysis outside the target region
503+
for (auto [opArg, blockArg] : llvm::zip_equal(
504+
targetOp.getMapVars(), argIface.getMapBlockArgs())) {
505+
if (blockArg == op.getMemref()) {
506+
omp::MapInfoOp mapInfo =
507+
llvm::cast<omp::MapInfoOp>(opArg.getDefiningOp());
508+
ompValArg = mapInfo.getVarPtr();
509+
break;
510+
}
511+
}
512+
// If given operation does not reflect mapping item,
513+
// check private clause
514+
if (!ompValArg)
515+
ompValArg = getPrivateArg(argIface, targetOp, op);
516+
})
517+
.template Case<omp::DistributeOp, omp::ParallelOp,
518+
omp::SectionsOp, omp::SimdOp, omp::SingleOp,
519+
omp::TaskloopOp, omp::TaskOp, omp::WsloopOp>(
520+
[&](auto privateOp) {
521+
ompValArg = getPrivateArg(argIface, privateOp, op);
522+
});
523+
if (ompValArg) {
524+
v = ompValArg;
525+
defOp = ompValArg.getDefiningOp();
526+
return;
487527
}
488528
}
489529
auto varIf = llvm::cast<fir::FortranVariableOpInterface>(defOp);
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Use --mlir-disable-threading so that the AA queries are serialized
2+
// as well as its diagnostic output.
3+
// RUN: fir-opt %s -pass-pipeline='builtin.module(func.func(test-fir-alias-analysis))' -split-input-file --mlir-disable-threading 2>&1 | FileCheck %s
4+
5+
// Fortran code:
6+
// program main
7+
// integer, target :: arrayA(10)
8+
// integer, pointer, dimension(:) :: ptrA
9+
// integer :: i
10+
// ptrA => arrayA
11+
// !$omp teams distribute parallel do firstprivate(ptrA)
12+
// do i = 1, 10
13+
// arrayA(i) = arrayA(i) + ptrA(i);
14+
// end do
15+
// end program main
16+
17+
// CHECK-LABEL: Testing : "_QQmain"
18+
// CHECK-DAG: ptrA#0 <-> ArrayA#0: MayAlias
19+
20+
omp.private {type = private} @_QFEi_private_ref_i32 : !fir.ref<i32> alloc {
21+
^bb0(%arg0: !fir.ref<i32>):
22+
%0 = fir.alloca i32 {bindc_name = "i", pinned, uniq_name = "_QFEi"}
23+
%1:2 = hlfir.declare %0 {uniq_name = "_QFEi"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
24+
omp.yield(%1#0 : !fir.ref<i32>)
25+
}
26+
omp.private {type = firstprivate} @_QFEptra_firstprivate_ref_box_ptr_Uxi32 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>> alloc {
27+
^bb0(%arg0: !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>):
28+
%0 = fir.alloca !fir.box<!fir.ptr<!fir.array<?xi32>>> {bindc_name = "ptra", pinned, uniq_name = "_QFEptra"}
29+
%1:2 = hlfir.declare %0 {fortran_attrs = #fir.var_attrs<pointer>, uniq_name = "_QFEptra"} : (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>) -> (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>, !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>)
30+
omp.yield(%1#0 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>)
31+
} copy {
32+
^bb0(%arg0: !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>, %arg1: !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>):
33+
%0 = fir.load %arg0 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>
34+
fir.store %0 to %arg1 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>
35+
omp.yield(%arg1 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>)
36+
}
37+
func.func @_QQmain() attributes {fir.bindc_name = "main"} {
38+
%0 = fir.address_of(@_QFEarraya) : !fir.ref<!fir.array<10xi32>>
39+
%c10 = arith.constant 10 : index
40+
%1 = fir.shape %c10 : (index) -> !fir.shape<1>
41+
%2:2 = hlfir.declare %0(%1) {fortran_attrs = #fir.var_attrs<target>, uniq_name = "_QFEarraya"} : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<10xi32>>, !fir.ref<!fir.array<10xi32>>)
42+
%3 = fir.address_of(@_QFEarrayb) : !fir.ref<!fir.array<10xi32>>
43+
%c10_0 = arith.constant 10 : index
44+
%4 = fir.shape %c10_0 : (index) -> !fir.shape<1>
45+
%5:2 = hlfir.declare %3(%4) {uniq_name = "_QFEarrayb"} : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<10xi32>>, !fir.ref<!fir.array<10xi32>>)
46+
%6 = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFEi"}
47+
%7:2 = hlfir.declare %6 {uniq_name = "_QFEi"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
48+
%8 = fir.address_of(@_QFEptra) : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>
49+
%9:2 = hlfir.declare %8 {fortran_attrs = #fir.var_attrs<pointer>, uniq_name = "_QFEptra"} : (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>) -> (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>, !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>)
50+
%10 = fir.shape %c10 : (index) -> !fir.shape<1>
51+
%11 = fir.embox %2#1(%10) : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>) -> !fir.box<!fir.ptr<!fir.array<?xi32>>>
52+
fir.store %11 to %9#1 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>
53+
omp.teams {
54+
omp.parallel private(@_QFEptra_firstprivate_ref_box_ptr_Uxi32 %9#0 -> %arg0, @_QFEi_private_ref_i32 %7#0 -> %arg1 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>, !fir.ref<i32>) {
55+
%12:2 = hlfir.declare %arg0 {fortran_attrs = #fir.var_attrs<pointer>, uniq_name = "_QFEptra"} : (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>) -> (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>, !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>)
56+
%13:2 = hlfir.declare %arg1 {uniq_name = "_QFEi"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
57+
%c1_i32 = arith.constant 1 : i32
58+
%c10_i32 = arith.constant 10 : i32
59+
%c1_i32_1 = arith.constant 1 : i32
60+
omp.distribute {
61+
omp.wsloop {
62+
omp.loop_nest (%arg2) : i32 = (%c1_i32) to (%c10_i32) inclusive step (%c1_i32_1) {
63+
fir.store %arg2 to %13#1 : !fir.ref<i32>
64+
%14 = fir.load %13#0 : !fir.ref<i32>
65+
%15 = fir.convert %14 : (i32) -> i64
66+
%16 = hlfir.designate %2#0 (%15) : (!fir.ref<!fir.array<10xi32>>, i64) -> !fir.ref<i32>
67+
%17 = fir.load %16 : !fir.ref<i32>
68+
%18 = fir.load %12#0 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>
69+
%19 = fir.load %13#0 : !fir.ref<i32>
70+
%20 = fir.convert %19 : (i32) -> i64
71+
%21 = hlfir.designate %18 (%20) {test.ptr = "ptrA" } : (!fir.box<!fir.ptr<!fir.array<?xi32>>>, i64) -> !fir.ref<i32>
72+
%22 = fir.load %21 : !fir.ref<i32>
73+
%23 = arith.addi %17, %22 : i32
74+
%24 = fir.load %13#0 : !fir.ref<i32>
75+
%25 = fir.convert %24 : (i32) -> i64
76+
%26 = hlfir.designate %2#0 (%25) {test.ptr = "ArrayA"} : (!fir.ref<!fir.array<10xi32>>, i64) -> !fir.ref<i32>
77+
hlfir.assign %23 to %26 : i32, !fir.ref<i32>
78+
omp.yield
79+
}
80+
} {omp.composite}
81+
} {omp.composite}
82+
omp.terminator
83+
} {omp.composite}
84+
omp.terminator
85+
}
86+
return
87+
}
88+
fir.global internal @_QFEarraya target : !fir.array<10xi32> {
89+
%0 = fir.zero_bits !fir.array<10xi32>
90+
fir.has_value %0 : !fir.array<10xi32>
91+
}
92+
fir.global internal @_QFEarrayb : !fir.array<10xi32> {
93+
%0 = fir.zero_bits !fir.array<10xi32>
94+
fir.has_value %0 : !fir.array<10xi32>
95+
}
96+
fir.global internal @_QFEptra : !fir.box<!fir.ptr<!fir.array<?xi32>>> {
97+
%0 = fir.zero_bits !fir.ptr<!fir.array<?xi32>>
98+
%c0 = arith.constant 0 : index
99+
%1 = fir.shape %c0 : (index) -> !fir.shape<1>
100+
%2 = fir.embox %0(%1) : (!fir.ptr<!fir.array<?xi32>>, !fir.shape<1>) -> !fir.box<!fir.ptr<!fir.array<?xi32>>>
101+
fir.has_value %2 : !fir.box<!fir.ptr<!fir.array<?xi32>>>
102+
}

0 commit comments

Comments
 (0)