Skip to content

Commit 351d651

Browse files
clang format
Created using spr 1.3.6
2 parents 0fb75f2 + 5cda242 commit 351d651

File tree

15 files changed

+514
-417
lines changed

15 files changed

+514
-417
lines changed

clang/lib/CIR/CodeGen/CIRGenOpenACCClause.cpp

Lines changed: 24 additions & 308 deletions
Large diffs are not rendered by default.
Lines changed: 323 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Emit OpenACC clause recipes as CIR code.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "CIRGenFunction.h"
14+
15+
#include "clang/AST/ASTContext.h"
16+
#include "clang/AST/DeclBase.h"
17+
#include "clang/AST/Expr.h"
18+
#include "clang/AST/ExprCXX.h"
19+
#include "clang/AST/TypeBase.h"
20+
#include "clang/Basic/OpenACCKinds.h"
21+
22+
#include "mlir/Dialect/OpenACC/OpenACC.h"
23+
24+
namespace clang::CIRGen {
25+
template <typename RecipeTy> class OpenACCRecipeBuilder {
26+
CIRGen::CIRGenFunction &cgf;
27+
CIRGen::CIRGenBuilderTy &builder;
28+
29+
mlir::acc::ReductionOperator convertReductionOp(OpenACCReductionOperator op) {
30+
switch (op) {
31+
case OpenACCReductionOperator::Addition:
32+
return mlir::acc::ReductionOperator::AccAdd;
33+
case OpenACCReductionOperator::Multiplication:
34+
return mlir::acc::ReductionOperator::AccMul;
35+
case OpenACCReductionOperator::Max:
36+
return mlir::acc::ReductionOperator::AccMax;
37+
case OpenACCReductionOperator::Min:
38+
return mlir::acc::ReductionOperator::AccMin;
39+
case OpenACCReductionOperator::BitwiseAnd:
40+
return mlir::acc::ReductionOperator::AccIand;
41+
case OpenACCReductionOperator::BitwiseOr:
42+
return mlir::acc::ReductionOperator::AccIor;
43+
case OpenACCReductionOperator::BitwiseXOr:
44+
return mlir::acc::ReductionOperator::AccXor;
45+
case OpenACCReductionOperator::And:
46+
return mlir::acc::ReductionOperator::AccLand;
47+
case OpenACCReductionOperator::Or:
48+
return mlir::acc::ReductionOperator::AccLor;
49+
case OpenACCReductionOperator::Invalid:
50+
llvm_unreachable("invalid reduction operator");
51+
}
52+
53+
llvm_unreachable("invalid reduction operator");
54+
}
55+
56+
std::string getRecipeName(SourceRange loc, QualType baseType,
57+
OpenACCReductionOperator reductionOp) {
58+
std::string recipeName;
59+
{
60+
llvm::raw_string_ostream stream(recipeName);
61+
62+
if constexpr (std::is_same_v<RecipeTy, mlir::acc::PrivateRecipeOp>) {
63+
stream << "privatization_";
64+
} else if constexpr (std::is_same_v<RecipeTy,
65+
mlir::acc::FirstprivateRecipeOp>) {
66+
stream << "firstprivatization_";
67+
68+
} else if constexpr (std::is_same_v<RecipeTy,
69+
mlir::acc::ReductionRecipeOp>) {
70+
stream << "reduction_";
71+
// Values here are a little weird (for bitwise and/or is 'i' prefix, and
72+
// logical ops with 'l'), but are chosen to be the same as the MLIR
73+
// dialect names as well as to match the Flang versions of these.
74+
switch (reductionOp) {
75+
case OpenACCReductionOperator::Addition:
76+
stream << "add_";
77+
break;
78+
case OpenACCReductionOperator::Multiplication:
79+
stream << "mul_";
80+
break;
81+
case OpenACCReductionOperator::Max:
82+
stream << "max_";
83+
break;
84+
case OpenACCReductionOperator::Min:
85+
stream << "min_";
86+
break;
87+
case OpenACCReductionOperator::BitwiseAnd:
88+
stream << "iand_";
89+
break;
90+
case OpenACCReductionOperator::BitwiseOr:
91+
stream << "ior_";
92+
break;
93+
case OpenACCReductionOperator::BitwiseXOr:
94+
stream << "xor_";
95+
break;
96+
case OpenACCReductionOperator::And:
97+
stream << "land_";
98+
break;
99+
case OpenACCReductionOperator::Or:
100+
stream << "lor_";
101+
break;
102+
case OpenACCReductionOperator::Invalid:
103+
llvm_unreachable("invalid reduction operator");
104+
}
105+
} else {
106+
static_assert(!sizeof(RecipeTy), "Unknown Recipe op kind");
107+
}
108+
109+
MangleContext &mc = cgf.cgm.getCXXABI().getMangleContext();
110+
mc.mangleCanonicalTypeName(baseType, stream);
111+
}
112+
return recipeName;
113+
}
114+
115+
void createFirstprivateRecipeCopy(
116+
mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp,
117+
CIRGenFunction::AutoVarEmission tempDeclEmission,
118+
mlir::acc::FirstprivateRecipeOp recipe, const VarDecl *varRecipe,
119+
const VarDecl *temporary) {
120+
mlir::Block *block = builder.createBlock(
121+
&recipe.getCopyRegion(), recipe.getCopyRegion().end(),
122+
{mainOp.getType(), mainOp.getType()}, {loc, loc});
123+
builder.setInsertionPointToEnd(&recipe.getCopyRegion().back());
124+
CIRGenFunction::LexicalScope ls(cgf, loc, block);
125+
126+
mlir::BlockArgument fromArg = block->getArgument(0);
127+
mlir::BlockArgument toArg = block->getArgument(1);
128+
129+
mlir::Type elementTy =
130+
mlir::cast<cir::PointerType>(mainOp.getType()).getPointee();
131+
132+
// Set the address of the emission to be the argument, so that we initialize
133+
// that instead of the variable in the other block.
134+
tempDeclEmission.setAllocatedAddress(
135+
Address{toArg, elementTy, cgf.getContext().getDeclAlign(varRecipe)});
136+
tempDeclEmission.EmittedAsOffload = true;
137+
138+
CIRGenFunction::DeclMapRevertingRAII declMapRAII{cgf, temporary};
139+
cgf.setAddrOfLocalVar(
140+
temporary,
141+
Address{fromArg, elementTy, cgf.getContext().getDeclAlign(varRecipe)});
142+
143+
cgf.emitAutoVarInit(tempDeclEmission);
144+
mlir::acc::YieldOp::create(builder, locEnd);
145+
}
146+
147+
// Create the 'init' section of the recipe, including the 'copy' section for
148+
// 'firstprivate'. Note that this function is not 'insertion point' clean, in
149+
// that it alters the insertion point to be inside of the 'destroy' section of
150+
// the recipe, but doesn't restore it aftewards.
151+
void createRecipeInitCopy(mlir::Location loc, mlir::Location locEnd,
152+
SourceRange exprRange, mlir::Value mainOp,
153+
RecipeTy recipe, const VarDecl *varRecipe,
154+
const VarDecl *temporary) {
155+
assert(varRecipe && "Required recipe variable not set?");
156+
157+
CIRGenFunction::AutoVarEmission tempDeclEmission{
158+
CIRGenFunction::AutoVarEmission::invalid()};
159+
CIRGenFunction::DeclMapRevertingRAII declMapRAII{cgf, varRecipe};
160+
161+
// Do the 'init' section of the recipe IR, which does an alloca, then the
162+
// initialization (except for firstprivate).
163+
mlir::Block *block = builder.createBlock(&recipe.getInitRegion(),
164+
recipe.getInitRegion().end(),
165+
{mainOp.getType()}, {loc});
166+
builder.setInsertionPointToEnd(&recipe.getInitRegion().back());
167+
CIRGenFunction::LexicalScope ls(cgf, loc, block);
168+
169+
tempDeclEmission =
170+
cgf.emitAutoVarAlloca(*varRecipe, builder.saveInsertionPoint());
171+
172+
// 'firstprivate' doesn't do its initialization in the 'init' section,
173+
// instead does it in the 'copy' section. SO only do init here.
174+
// 'reduction' appears to use it too (rather than a 'copy' section), so
175+
// we probably have to do it here too, but we can do that when we get to
176+
// reduction implementation.
177+
if constexpr (std::is_same_v<RecipeTy, mlir::acc::PrivateRecipeOp>) {
178+
// We are OK with no init for builtins, arrays of builtins, or pointers,
179+
// else we should NYI so we know to go look for these.
180+
if (cgf.getContext().getLangOpts().CPlusPlus &&
181+
!varRecipe->getType()
182+
->getPointeeOrArrayElementType()
183+
->isBuiltinType() &&
184+
!varRecipe->getType()->isPointerType() && !varRecipe->getInit()) {
185+
// If we don't have any initialization recipe, we failed during Sema to
186+
// initialize this correctly. If we disable the
187+
// Sema::TentativeAnalysisScopes in SemaOpenACC::CreateInitRecipe, it'll
188+
// emit an error to tell us. However, emitting those errors during
189+
// production is a violation of the standard, so we cannot do them.
190+
cgf.cgm.errorNYI(exprRange, "private default-init recipe");
191+
}
192+
cgf.emitAutoVarInit(tempDeclEmission);
193+
} else if constexpr (std::is_same_v<RecipeTy,
194+
mlir::acc::ReductionRecipeOp>) {
195+
// Unlike Private, the recipe here is always required as it has to do
196+
// init, not just 'default' init.
197+
if (!varRecipe->getInit())
198+
cgf.cgm.errorNYI(exprRange, "reduction init recipe");
199+
cgf.emitAutoVarInit(tempDeclEmission);
200+
}
201+
202+
mlir::acc::YieldOp::create(builder, locEnd);
203+
204+
if constexpr (std::is_same_v<RecipeTy, mlir::acc::FirstprivateRecipeOp>) {
205+
if (!varRecipe->getInit()) {
206+
// If we don't have any initialization recipe, we failed during Sema to
207+
// initialize this correctly. If we disable the
208+
// Sema::TentativeAnalysisScopes in SemaOpenACC::CreateInitRecipe, it'll
209+
// emit an error to tell us. However, emitting those errors during
210+
// production is a violation of the standard, so we cannot do them.
211+
cgf.cgm.errorNYI(
212+
exprRange, "firstprivate copy-init recipe not properly generated");
213+
}
214+
215+
createFirstprivateRecipeCopy(loc, locEnd, mainOp, tempDeclEmission,
216+
recipe, varRecipe, temporary);
217+
}
218+
}
219+
220+
// This function generates the 'combiner' section for a reduction recipe. Note
221+
// that this function is not 'insertion point' clean, in that it alters the
222+
// insertion point to be inside of the 'combiner' section of the recipe, but
223+
// doesn't restore it aftewards.
224+
void createReductionRecipeCombiner(mlir::Location loc, mlir::Location locEnd,
225+
mlir::Value mainOp,
226+
mlir::acc::ReductionRecipeOp recipe) {
227+
mlir::Block *block = builder.createBlock(
228+
&recipe.getCombinerRegion(), recipe.getCombinerRegion().end(),
229+
{mainOp.getType(), mainOp.getType()}, {loc, loc});
230+
builder.setInsertionPointToEnd(&recipe.getCombinerRegion().back());
231+
CIRGenFunction::LexicalScope ls(cgf, loc, block);
232+
233+
mlir::BlockArgument lhsArg = block->getArgument(0);
234+
235+
mlir::acc::YieldOp::create(builder, locEnd, lhsArg);
236+
}
237+
238+
// This function generates the 'destroy' section for a recipe. Note
239+
// that this function is not 'insertion point' clean, in that it alters the
240+
// insertion point to be inside of the 'destroy' section of the recipe, but
241+
// doesn't restore it aftewards.
242+
void createRecipeDestroySection(mlir::Location loc, mlir::Location locEnd,
243+
mlir::Value mainOp, CharUnits alignment,
244+
QualType baseType,
245+
mlir::Region &destroyRegion) {
246+
mlir::Block *block =
247+
builder.createBlock(&destroyRegion, destroyRegion.end(),
248+
{mainOp.getType(), mainOp.getType()}, {loc, loc});
249+
builder.setInsertionPointToEnd(&destroyRegion.back());
250+
CIRGenFunction::LexicalScope ls(cgf, loc, block);
251+
252+
mlir::Type elementTy =
253+
mlir::cast<cir::PointerType>(mainOp.getType()).getPointee();
254+
// The destroy region has a signature of "original item, privatized item".
255+
// So the 2nd item is the one that needs destroying, the former is just for
256+
// reference and we don't really have a need for it at the moment.
257+
Address addr{block->getArgument(1), elementTy, alignment};
258+
cgf.emitDestroy(addr, baseType,
259+
cgf.getDestroyer(QualType::DK_cxx_destructor));
260+
261+
mlir::acc::YieldOp::create(builder, locEnd);
262+
}
263+
264+
public:
265+
OpenACCRecipeBuilder(CIRGen::CIRGenFunction &cgf,
266+
CIRGen::CIRGenBuilderTy &builder)
267+
: cgf(cgf), builder(builder) {}
268+
RecipeTy getOrCreateRecipe(ASTContext &astCtx, const Expr *varRef,
269+
const VarDecl *varRecipe, const VarDecl *temporary,
270+
OpenACCReductionOperator reductionOp,
271+
DeclContext *dc, QualType baseType,
272+
mlir::Value mainOp) {
273+
274+
if (baseType->isPointerType() ||
275+
(baseType->isArrayType() && !baseType->isConstantArrayType())) {
276+
// It is clear that the use of pointers/VLAs in a recipe are not properly
277+
// generated/don't do what they are supposed to do. In the case where we
278+
// have 'bounds', we can actually figure out what we want to
279+
// initialize/copy/destroy/compare/etc, but we haven't figured out how
280+
// that looks yet, both between the IR and generation code. For now, we
281+
// will do an NYI error no it.
282+
cgf.cgm.errorNYI(
283+
varRef->getSourceRange(),
284+
"OpenACC recipe generation for pointer/non-constant arrays");
285+
}
286+
287+
mlir::ModuleOp mod = builder.getBlock()
288+
->getParent()
289+
->template getParentOfType<mlir::ModuleOp>();
290+
291+
std::string recipeName =
292+
getRecipeName(varRef->getSourceRange(), baseType, reductionOp);
293+
if (auto recipe = mod.lookupSymbol<RecipeTy>(recipeName))
294+
return recipe;
295+
296+
mlir::Location loc = cgf.cgm.getLoc(varRef->getBeginLoc());
297+
mlir::Location locEnd = cgf.cgm.getLoc(varRef->getEndLoc());
298+
299+
mlir::OpBuilder modBuilder(mod.getBodyRegion());
300+
RecipeTy recipe;
301+
302+
if constexpr (std::is_same_v<RecipeTy, mlir::acc::ReductionRecipeOp>) {
303+
recipe = RecipeTy::create(modBuilder, loc, recipeName, mainOp.getType(),
304+
convertReductionOp(reductionOp));
305+
} else {
306+
recipe = RecipeTy::create(modBuilder, loc, recipeName, mainOp.getType());
307+
}
308+
309+
createRecipeInitCopy(loc, locEnd, varRef->getSourceRange(), mainOp, recipe,
310+
varRecipe, temporary);
311+
312+
if constexpr (std::is_same_v<RecipeTy, mlir::acc::ReductionRecipeOp>) {
313+
createReductionRecipeCombiner(loc, locEnd, mainOp, recipe);
314+
}
315+
316+
if (varRecipe && varRecipe->needsDestruction(cgf.getContext()))
317+
createRecipeDestroySection(loc, locEnd, mainOp,
318+
cgf.getContext().getDeclAlign(varRecipe),
319+
baseType, recipe.getDestroyRegion());
320+
return recipe;
321+
}
322+
};
323+
} // namespace clang::CIRGen

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -844,8 +844,7 @@ void CompilerInstance::setOutputManager(
844844

845845
void CompilerInstance::createOutputManager() {
846846
assert(!OutputMgr && "Already has an output manager");
847-
OutputMgr =
848-
llvm::makeIntrusiveRefCnt<llvm::vfs::OnDiskOutputBackend>();
847+
OutputMgr = llvm::makeIntrusiveRefCnt<llvm::vfs::OnDiskOutputBackend>();
849848
}
850849

851850
llvm::vfs::OutputBackend &CompilerInstance::getOutputManager() {

lld/COFF/LTO.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/Support/Caching.h"
2727
#include "llvm/Support/CodeGen.h"
2828
#include "llvm/Support/MemoryBuffer.h"
29+
#include "llvm/Support/TimeProfiler.h"
2930
#include "llvm/Support/raw_ostream.h"
3031
#include <cstddef>
3132
#include <memory>
@@ -176,6 +177,7 @@ void BitcodeCompiler::add(BitcodeFile &f) {
176177
// Merge all the bitcode files we have seen, codegen the result
177178
// and return the resulting objects.
178179
std::vector<InputFile *> BitcodeCompiler::compile() {
180+
llvm::TimeTraceScope timeScope("Bitcode compile");
179181
unsigned maxTasks = ltoObj->getMaxTasks();
180182
buf.resize(maxTasks);
181183
files.resize(maxTasks);

lld/COFF/SymbolTable.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,11 +1437,13 @@ void SymbolTable::compileBitcodeFiles() {
14371437
if (bitcodeFileInstances.empty())
14381438
return;
14391439

1440-
llvm::TimeTraceScope timeScope("Compile bitcode");
14411440
ScopedTimer t(ctx.ltoTimer);
14421441
lto.reset(new BitcodeCompiler(ctx));
1443-
for (BitcodeFile *f : bitcodeFileInstances)
1444-
lto->add(*f);
1442+
{
1443+
llvm::TimeTraceScope addScope("Add bitcode file instances");
1444+
for (BitcodeFile *f : bitcodeFileInstances)
1445+
lto->add(*f);
1446+
}
14451447
for (InputFile *newObj : lto->compile()) {
14461448
ObjFile *obj = cast<ObjFile>(newObj);
14471449
obj->parse();

llvm/lib/Bitcode/Reader/MetadataLoader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "llvm/Support/CommandLine.h"
4444
#include "llvm/Support/Compiler.h"
4545
#include "llvm/Support/ErrorHandling.h"
46+
#include "llvm/Support/TimeProfiler.h"
4647

4748
#include <algorithm>
4849
#include <cassert>
@@ -1052,6 +1053,7 @@ void MetadataLoader::MetadataLoaderImpl::callMDTypeCallback(Metadata **Val,
10521053
/// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
10531054
/// module level metadata.
10541055
Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) {
1056+
llvm::TimeTraceScope timeScope("Parse metadata");
10551057
if (!ModuleLevel && MetadataList.hasFwdRefs())
10561058
return error("Invalid metadata: fwd refs into function blocks");
10571059

llvm/lib/IR/AutoUpgrade.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "llvm/Support/ErrorHandling.h"
4949
#include "llvm/Support/NVPTXAddrSpace.h"
5050
#include "llvm/Support/Regex.h"
51+
#include "llvm/Support/TimeProfiler.h"
5152
#include "llvm/TargetParser/Triple.h"
5253
#include <cstdint>
5354
#include <cstring>
@@ -5256,6 +5257,7 @@ bool llvm::UpgradeDebugInfo(Module &M) {
52565257
if (DisableAutoUpgradeDebugInfo)
52575258
return false;
52585259

5260+
llvm::TimeTraceScope timeScope("Upgrade debug info");
52595261
// We need to get metadata before the module is verified (i.e., getModuleFlag
52605262
// makes assumptions that we haven't verified yet). Carefully extract the flag
52615263
// from the metadata.

0 commit comments

Comments
 (0)