-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[AMDGPU] Add AMDGPU specific variadic operation MCExprs #82022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3d03a58
AMDGPU Variadic MCExprs
JanekvO 7eaae11
Apply feedback
JanekvO d9b0a9b
Feedback, use optional for MCExpr instead of INT64_MIN
JanekvO fdd6c61
Feedback, add line numbers to test.
JanekvO 5df4a04
Feedback, add check for number of commas with tests
JanekvO 67b279e
Feedback
JanekvO 9f8d4af
Feedback, change mcexpr from logical to bitwise or, documentation on …
JanekvO ade9a39
Feedback, documentation modifications
JanekvO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
//===- AMDGPUMCExpr.cpp - AMDGPU specific MC expression classes -----------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "AMDGPUMCExpr.h" | ||
#include "llvm/MC/MCContext.h" | ||
#include "llvm/MC/MCStreamer.h" | ||
#include "llvm/MC/MCSymbol.h" | ||
#include "llvm/MC/MCValue.h" | ||
#include "llvm/Support/Allocator.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
#include <optional> | ||
|
||
using namespace llvm; | ||
|
||
const AMDGPUVariadicMCExpr * | ||
AMDGPUVariadicMCExpr::create(VariadicKind Kind, ArrayRef<const MCExpr *> Args, | ||
MCContext &Ctx) { | ||
return new (Ctx) AMDGPUVariadicMCExpr(Kind, Args); | ||
} | ||
|
||
const MCExpr *AMDGPUVariadicMCExpr::getSubExpr(size_t Index) const { | ||
assert(Index < Args.size() && | ||
"Indexing out of bounds AMDGPUVariadicMCExpr sub-expr"); | ||
return Args[Index]; | ||
} | ||
|
||
void AMDGPUVariadicMCExpr::printImpl(raw_ostream &OS, | ||
const MCAsmInfo *MAI) const { | ||
switch (Kind) { | ||
default: | ||
llvm_unreachable("Unknown AMDGPUVariadicMCExpr kind."); | ||
case AGVK_Or: | ||
OS << "or("; | ||
break; | ||
case AGVK_Max: | ||
OS << "max("; | ||
break; | ||
} | ||
for (auto It = Args.begin(); It != Args.end(); ++It) { | ||
(*It)->print(OS, MAI, /*InParens=*/false); | ||
if ((It + 1) != Args.end()) | ||
OS << ", "; | ||
} | ||
OS << ')'; | ||
} | ||
|
||
static int64_t op(AMDGPUVariadicMCExpr::VariadicKind Kind, int64_t Arg1, | ||
int64_t Arg2) { | ||
switch (Kind) { | ||
default: | ||
llvm_unreachable("Unknown AMDGPUVariadicMCExpr kind."); | ||
case AMDGPUVariadicMCExpr::AGVK_Max: | ||
return std::max(Arg1, Arg2); | ||
case AMDGPUVariadicMCExpr::AGVK_Or: | ||
return Arg1 | Arg2; | ||
} | ||
} | ||
|
||
bool AMDGPUVariadicMCExpr::evaluateAsRelocatableImpl( | ||
MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const { | ||
std::optional<int64_t> Total; | ||
|
||
for (const MCExpr *Arg : Args) { | ||
MCValue ArgRes; | ||
if (!Arg->evaluateAsRelocatable(ArgRes, Layout, Fixup) || | ||
!ArgRes.isAbsolute()) | ||
return false; | ||
|
||
if (!Total.has_value()) | ||
Total = ArgRes.getConstant(); | ||
Total = op(Kind, *Total, ArgRes.getConstant()); | ||
} | ||
|
||
Res = MCValue::get(*Total); | ||
return true; | ||
} | ||
|
||
void AMDGPUVariadicMCExpr::visitUsedExpr(MCStreamer &Streamer) const { | ||
for (const MCExpr *Arg : Args) | ||
Streamer.visitUsedExpr(*Arg); | ||
} | ||
|
||
MCFragment *AMDGPUVariadicMCExpr::findAssociatedFragment() const { | ||
for (const MCExpr *Arg : Args) { | ||
if (Arg->findAssociatedFragment()) | ||
return Arg->findAssociatedFragment(); | ||
} | ||
return nullptr; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//===- AMDGPUMCExpr.h - AMDGPU specific MC expression classes ---*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUMCEXPR_H | ||
#define LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUMCEXPR_H | ||
|
||
#include "llvm/ADT/ArrayRef.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/MC/MCExpr.h" | ||
|
||
namespace llvm { | ||
|
||
/// AMDGPU target specific variadic MCExpr operations. | ||
/// | ||
/// Takes in a minimum of 1 argument to be used with an operation. The supported | ||
/// operations are: | ||
/// - (bitwise) or | ||
/// - max | ||
/// | ||
JanekvO marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// \note If the 'or'/'max' operations are provided only a single argument, the | ||
/// operation will act as a no-op and simply resolve as the provided argument. | ||
/// | ||
class AMDGPUVariadicMCExpr : public MCTargetExpr { | ||
public: | ||
enum VariadicKind { AGVK_None, AGVK_Or, AGVK_Max }; | ||
|
||
private: | ||
VariadicKind Kind; | ||
SmallVector<const MCExpr *, 2> Args; | ||
|
||
AMDGPUVariadicMCExpr(VariadicKind Kind, ArrayRef<const MCExpr *> Args) | ||
: Kind(Kind), Args(Args) { | ||
assert(Args.size() >= 1 && "Needs a minimum of one expression."); | ||
assert(Kind != AGVK_None && | ||
"Cannot construct AMDGPUVariadicMCExpr of kind none."); | ||
} | ||
|
||
public: | ||
static const AMDGPUVariadicMCExpr * | ||
create(VariadicKind Kind, ArrayRef<const MCExpr *> Args, MCContext &Ctx); | ||
|
||
static const AMDGPUVariadicMCExpr *createOr(ArrayRef<const MCExpr *> Args, | ||
MCContext &Ctx) { | ||
return create(VariadicKind::AGVK_Or, Args, Ctx); | ||
} | ||
|
||
static const AMDGPUVariadicMCExpr *createMax(ArrayRef<const MCExpr *> Args, | ||
MCContext &Ctx) { | ||
return create(VariadicKind::AGVK_Max, Args, Ctx); | ||
} | ||
|
||
VariadicKind getKind() const { return Kind; } | ||
const MCExpr *getSubExpr(size_t Index) const; | ||
|
||
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override; | ||
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, | ||
const MCFixup *Fixup) const override; | ||
void visitUsedExpr(MCStreamer &Streamer) const override; | ||
MCFragment *findAssociatedFragment() const override; | ||
void fixELFSymbolsInTLSFixups(MCAssembler &) const override{}; | ||
|
||
static bool classof(const MCExpr *E) { | ||
JanekvO marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return E->getKind() == MCExpr::Target; | ||
} | ||
}; | ||
|
||
} // end namespace llvm | ||
|
||
#endif // LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUMCEXPR_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.