Skip to content

Commit 6f44ba8

Browse files
authored
merge main into amd-staging (#346)
2 parents e73d555 + ff37c15 commit 6f44ba8

File tree

173 files changed

+9117
-3925
lines changed

Some content is hidden

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

173 files changed

+9117
-3925
lines changed

clang/docs/AllocToken.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ The default mode to calculate tokens is:
3737
pointers.
3838

3939
Other token ID assignment modes are supported, but they may be subject to
40-
change or removal. These may (experimentally) be selected with ``-mllvm
41-
-alloc-token-mode=<mode>``:
40+
change or removal. These may (experimentally) be selected with ``-Xclang
41+
-falloc-token-mode=<mode>``:
4242

4343
* ``typehash``: This mode assigns a token ID based on the hash of the allocated
4444
type's name.

clang/include/clang/AST/StmtOpenACC.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,17 @@ class OpenACCAtomicConstruct final
815815
Stmt *getAssociatedStmt() {
816816
return OpenACCAssociatedStmtConstruct::getAssociatedStmt();
817817
}
818+
819+
// A struct to represent a broken-down version of the associated statement,
820+
// providing the information specified in OpenACC3.3 Section 2.12.
821+
struct StmtInfo {
822+
const Expr *V;
823+
const Expr *X;
824+
// TODO: OpenACC: We should expand this as we're implementing the other
825+
// atomic construct kinds.
826+
};
827+
828+
const StmtInfo getAssociatedStmtInfo() const;
818829
};
819830

820831
} // namespace clang

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,6 @@ class CodeGenOptions : public CodeGenOptionsBase {
455455

456456
std::optional<double> AllowRuntimeCheckSkipHotCutoff;
457457

458-
/// Maximum number of allocation tokens (0 = no max), nullopt if none set (use
459-
/// pass default).
460-
std::optional<uint64_t> AllocTokenMax;
461-
462458
/// List of backend command-line options for -fembed-bitcode.
463459
std::vector<uint8_t> CmdArgs;
464460

clang/include/clang/Basic/LangOptions.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/ADT/FloatingPointMode.h"
2626
#include "llvm/ADT/StringRef.h"
2727
#include "llvm/BinaryFormat/DXContainer.h"
28+
#include "llvm/Support/AllocToken.h"
2829
#include "llvm/TargetParser/Triple.h"
2930
#include <optional>
3031
#include <string>
@@ -565,6 +566,13 @@ class LangOptions : public LangOptionsBase {
565566
bool AtomicFineGrainedMemory = false;
566567
bool AtomicIgnoreDenormalMode = false;
567568

569+
/// Maximum number of allocation tokens (0 = no max), nullopt if none set (use
570+
/// target default).
571+
std::optional<uint64_t> AllocTokenMax;
572+
573+
/// The allocation token mode.
574+
std::optional<llvm::AllocTokenMode> AllocTokenMode;
575+
568576
LangOptions();
569577

570578
/// Set language defaults for the given input language and

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2760,6 +2760,10 @@ def falloc_token_max_EQ : Joined<["-"], "falloc-token-max=">,
27602760
MetaVarName<"<N>">,
27612761
HelpText<"Limit to maximum N allocation tokens (0 = no max)">;
27622762

2763+
def falloc_token_mode_EQ : Joined<["-"], "falloc-token-mode=">,
2764+
Group<f_Group>, Visibility<[CC1Option]>,
2765+
HelpText<"Set the allocation token mode (experimental)">;
2766+
27632767
def fallow_runtime_check_skip_hot_cutoff_EQ
27642768
: Joined<["-"], "fallow-runtime-check-skip-hot-cutoff=">,
27652769
Group<f_clang_Group>,

clang/lib/AST/StmtOpenACC.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
#include "clang/AST/StmtOpenACC.h"
1414
#include "clang/AST/ASTContext.h"
15+
#include "clang/AST/ExprCXX.h"
1516
#include "clang/AST/StmtCXX.h"
17+
1618
using namespace clang;
1719

1820
OpenACCComputeConstruct *
@@ -322,6 +324,38 @@ OpenACCAtomicConstruct *OpenACCAtomicConstruct::Create(
322324
return Inst;
323325
}
324326

327+
const OpenACCAtomicConstruct::StmtInfo
328+
OpenACCAtomicConstruct::getAssociatedStmtInfo() const {
329+
// This ends up being a vastly simplified version of SemaOpenACCAtomic, since
330+
// it doesn't have to worry about erroring out, but we should do a lot of
331+
// asserts to ensure we don't get off into the weeds.
332+
assert(getAssociatedStmt() && "invalid associated stmt?");
333+
334+
switch (AtomicKind) {
335+
case OpenACCAtomicKind::None:
336+
case OpenACCAtomicKind::Write:
337+
case OpenACCAtomicKind::Update:
338+
case OpenACCAtomicKind::Capture:
339+
assert(false && "Only 'read' has been implemented here");
340+
return {};
341+
case OpenACCAtomicKind::Read: {
342+
// Read only supports the format 'v = x'; where both sides are a scalar
343+
// expression. This can come in 2 forms; BinaryOperator or
344+
// CXXOperatorCallExpr (rarely).
345+
const Expr *AssignExpr = cast<const Expr>(getAssociatedStmt());
346+
if (const auto *BO = dyn_cast<BinaryOperator>(AssignExpr)) {
347+
assert(BO->getOpcode() == BO_Assign);
348+
return {BO->getLHS()->IgnoreImpCasts(), BO->getRHS()->IgnoreImpCasts()};
349+
}
350+
351+
const auto *OO = cast<CXXOperatorCallExpr>(AssignExpr);
352+
assert(OO->getOperator() == OO_Equal);
353+
354+
return {OO->getArg(0)->IgnoreImpCasts(), OO->getArg(1)->IgnoreImpCasts()};
355+
}
356+
}
357+
}
358+
325359
OpenACCCacheConstruct *OpenACCCacheConstruct::CreateEmpty(const ASTContext &C,
326360
unsigned NumVars) {
327361
void *Mem =

clang/lib/CIR/CodeGen/CIRGenOpenACCClause.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,15 @@ class OpenACCClauseCIREmitter final
553553
}
554554

555555
void VisitIfClause(const OpenACCIfClause &clause) {
556-
if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
557-
mlir::acc::KernelsOp, mlir::acc::InitOp,
558-
mlir::acc::ShutdownOp, mlir::acc::SetOp,
559-
mlir::acc::DataOp, mlir::acc::WaitOp,
560-
mlir::acc::HostDataOp, mlir::acc::EnterDataOp,
561-
mlir::acc::ExitDataOp, mlir::acc::UpdateOp>) {
556+
if constexpr (isOneOfTypes<
557+
OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
558+
mlir::acc::KernelsOp, mlir::acc::InitOp,
559+
mlir::acc::ShutdownOp, mlir::acc::SetOp,
560+
mlir::acc::DataOp, mlir::acc::WaitOp,
561+
mlir::acc::HostDataOp, mlir::acc::EnterDataOp,
562+
mlir::acc::ExitDataOp, mlir::acc::UpdateOp,
563+
mlir::acc::AtomicReadOp, mlir::acc::AtomicWriteOp,
564+
mlir::acc::AtomicUpdateOp, mlir::acc::AtomicCaptureOp>) {
562565
operation.getIfCondMutable().append(
563566
createCondition(clause.getConditionExpr()));
564567
} else if constexpr (isCombinedType<OpTy>) {
@@ -1144,6 +1147,10 @@ EXPL_SPEC(mlir::acc::HostDataOp)
11441147
EXPL_SPEC(mlir::acc::EnterDataOp)
11451148
EXPL_SPEC(mlir::acc::ExitDataOp)
11461149
EXPL_SPEC(mlir::acc::UpdateOp)
1150+
EXPL_SPEC(mlir::acc::AtomicReadOp)
1151+
EXPL_SPEC(mlir::acc::AtomicWriteOp)
1152+
EXPL_SPEC(mlir::acc::AtomicCaptureOp)
1153+
EXPL_SPEC(mlir::acc::AtomicUpdateOp)
11471154
#undef EXPL_SPEC
11481155

11491156
template <typename ComputeOp, typename LoopOp>

clang/lib/CIR/CodeGen/CIRGenStmtOpenACC.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,29 @@ CIRGenFunction::emitOpenACCCacheConstruct(const OpenACCCacheConstruct &s) {
306306

307307
mlir::LogicalResult
308308
CIRGenFunction::emitOpenACCAtomicConstruct(const OpenACCAtomicConstruct &s) {
309-
cgm.errorNYI(s.getSourceRange(), "OpenACC Atomic Construct");
310-
return mlir::failure();
309+
// For now, we are only support 'read', so diagnose. We can switch on the kind
310+
// later once we start implementing the other 3 forms.
311+
if (s.getAtomicKind() != OpenACCAtomicKind::Read) {
312+
cgm.errorNYI(s.getSourceRange(), "OpenACC Atomic Construct");
313+
return mlir::failure();
314+
}
315+
316+
// While Atomic is an 'associated statement' construct, it 'steals' the
317+
// expression it is associated with rather than emitting it inside of it. So
318+
// it has custom emit logic.
319+
mlir::Location start = getLoc(s.getSourceRange().getBegin());
320+
OpenACCAtomicConstruct::StmtInfo inf = s.getAssociatedStmtInfo();
321+
// Atomic 'read' only permits 'v = x', where v and x are both scalar L values.
322+
// The getAssociatedStmtInfo strips off implicit casts, which includes
323+
// implicit conversions and L-to-R-Value conversions, so we can just emit it
324+
// as an L value. The Flang implementation has no problem with different
325+
// types, so it appears that the dialect can handle the conversions.
326+
mlir::Value v = emitLValue(inf.V).getPointer();
327+
mlir::Value x = emitLValue(inf.X).getPointer();
328+
mlir::Type resTy = convertType(inf.V->getType());
329+
auto op = mlir::acc::AtomicReadOp::create(builder, start, x, v, resTy,
330+
/*ifCond=*/{});
331+
emitOpenACCClauses(op, s.getDirectiveKind(), s.getDirectiveLoc(),
332+
s.clauses());
333+
return mlir::success();
311334
}

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,12 @@ class EmitAssemblyHelper {
234234
};
235235
} // namespace
236236

237-
static AllocTokenOptions getAllocTokenOptions(const CodeGenOptions &CGOpts) {
237+
static AllocTokenOptions getAllocTokenOptions(const LangOptions &LangOpts,
238+
const CodeGenOptions &CGOpts) {
238239
AllocTokenOptions Opts;
239-
Opts.MaxTokens = CGOpts.AllocTokenMax;
240+
if (LangOpts.AllocTokenMode)
241+
Opts.Mode = *LangOpts.AllocTokenMode;
242+
Opts.MaxTokens = LangOpts.AllocTokenMax;
240243
Opts.Extended = CGOpts.SanitizeAllocTokenExtended;
241244
Opts.FastABI = CGOpts.SanitizeAllocTokenFastABI;
242245
return Opts;
@@ -802,7 +805,7 @@ static void addSanitizers(const Triple &TargetTriple,
802805
// memory allocation function detection.
803806
MPM.addPass(InferFunctionAttrsPass());
804807
}
805-
MPM.addPass(AllocTokenPass(getAllocTokenOptions(CodeGenOpts)));
808+
MPM.addPass(AllocTokenPass(getAllocTokenOptions(LangOpts, CodeGenOpts)));
806809
}
807810
};
808811
if (ClSanitizeOnOptimizerEarlyEP) {

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,10 +1841,6 @@ void CompilerInvocationBase::GenerateCodeGenArgs(const CodeGenOptions &Opts,
18411841
serializeSanitizerKinds(Opts.SanitizeAnnotateDebugInfo))
18421842
GenerateArg(Consumer, OPT_fsanitize_annotate_debug_info_EQ, Sanitizer);
18431843

1844-
if (Opts.AllocTokenMax)
1845-
GenerateArg(Consumer, OPT_falloc_token_max_EQ,
1846-
std::to_string(*Opts.AllocTokenMax));
1847-
18481844
if (!Opts.EmitVersionIdentMetadata)
18491845
GenerateArg(Consumer, OPT_Qn);
18501846

@@ -2358,15 +2354,6 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
23582354
}
23592355
}
23602356

2361-
if (const auto *Arg = Args.getLastArg(options::OPT_falloc_token_max_EQ)) {
2362-
StringRef S = Arg->getValue();
2363-
uint64_t Value = 0;
2364-
if (S.getAsInteger(0, Value))
2365-
Diags.Report(diag::err_drv_invalid_value) << Arg->getAsString(Args) << S;
2366-
else
2367-
Opts.AllocTokenMax = Value;
2368-
}
2369-
23702357
Opts.EmitVersionIdentMetadata = Args.hasFlag(OPT_Qy, OPT_Qn, true);
23712358

23722359
if (!LangOpts->CUDAIsDevice)
@@ -4037,6 +4024,29 @@ void CompilerInvocationBase::GenerateLangArgs(const LangOptions &Opts,
40374024

40384025
if (!Opts.RandstructSeed.empty())
40394026
GenerateArg(Consumer, OPT_frandomize_layout_seed_EQ, Opts.RandstructSeed);
4027+
4028+
if (Opts.AllocTokenMax)
4029+
GenerateArg(Consumer, OPT_falloc_token_max_EQ,
4030+
std::to_string(*Opts.AllocTokenMax));
4031+
4032+
if (Opts.AllocTokenMode) {
4033+
StringRef S;
4034+
switch (*Opts.AllocTokenMode) {
4035+
case llvm::AllocTokenMode::Increment:
4036+
S = "increment";
4037+
break;
4038+
case llvm::AllocTokenMode::Random:
4039+
S = "random";
4040+
break;
4041+
case llvm::AllocTokenMode::TypeHash:
4042+
S = "typehash";
4043+
break;
4044+
case llvm::AllocTokenMode::TypeHashPointerSplit:
4045+
S = "typehashpointersplit";
4046+
break;
4047+
}
4048+
GenerateArg(Consumer, OPT_falloc_token_mode_EQ, S);
4049+
}
40404050
}
40414051

40424052
bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
@@ -4673,6 +4683,23 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
46734683
if (const Arg *A = Args.getLastArg(OPT_frandomize_layout_seed_EQ))
46744684
Opts.RandstructSeed = A->getValue(0);
46754685

4686+
if (const auto *Arg = Args.getLastArg(options::OPT_falloc_token_max_EQ)) {
4687+
StringRef S = Arg->getValue();
4688+
uint64_t Value = 0;
4689+
if (S.getAsInteger(0, Value))
4690+
Diags.Report(diag::err_drv_invalid_value) << Arg->getAsString(Args) << S;
4691+
else
4692+
Opts.AllocTokenMax = Value;
4693+
}
4694+
4695+
if (const auto *Arg = Args.getLastArg(options::OPT_falloc_token_mode_EQ)) {
4696+
StringRef S = Arg->getValue();
4697+
if (auto Mode = getAllocTokenModeFromString(S))
4698+
Opts.AllocTokenMode = Mode;
4699+
else
4700+
Diags.Report(diag::err_drv_invalid_value) << Arg->getAsString(Args) << S;
4701+
}
4702+
46764703
// Validate options for HLSL
46774704
if (Opts.HLSL) {
46784705
// TODO: Revisit restricting SPIR-V to logical once we've figured out how to

0 commit comments

Comments
 (0)