2017-08-10 08:46:15 +08:00
|
|
|
//===- SIMemoryLegalizer.cpp ----------------------------------------------===//
|
2017-07-22 05:19:23 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
/// \brief Memory legalizer - implements memory model. More information can be
|
|
|
|
/// found here:
|
|
|
|
/// http://llvm.org/docs/AMDGPUUsage.html#memory-model
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AMDGPU.h"
|
|
|
|
#include "AMDGPUMachineModuleInfo.h"
|
|
|
|
#include "AMDGPUSubtarget.h"
|
2017-08-10 08:46:15 +08:00
|
|
|
#include "SIDefines.h"
|
|
|
|
#include "SIInstrInfo.h"
|
2017-07-22 05:19:23 +08:00
|
|
|
#include "Utils/AMDGPUBaseInfo.h"
|
2017-08-10 08:46:15 +08:00
|
|
|
#include "llvm/ADT/None.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2017-07-22 05:19:23 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2017-08-10 08:46:15 +08:00
|
|
|
#include "llvm/CodeGen/MachineMemOperand.h"
|
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
|
|
|
#include "llvm/IR/DebugLoc.h"
|
2017-07-22 05:19:23 +08:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
2017-08-10 08:46:15 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/MC/MCInstrDesc.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/AtomicOrdering.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <list>
|
2017-07-22 05:19:23 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::AMDGPU;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "si-memory-legalizer"
|
|
|
|
#define PASS_NAME "SI Memory Legalizer"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-09-06 00:41:25 +08:00
|
|
|
class SIMemOpInfo final {
|
|
|
|
private:
|
2017-09-06 00:18:05 +08:00
|
|
|
SyncScope::ID SSID = SyncScope::System;
|
2017-09-06 03:01:10 +08:00
|
|
|
AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
|
|
|
|
AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic;
|
2017-09-08 01:14:54 +08:00
|
|
|
bool IsNonTemporal = false;
|
2017-07-22 05:19:23 +08:00
|
|
|
|
2017-09-06 03:01:10 +08:00
|
|
|
SIMemOpInfo(SyncScope::ID SSID, AtomicOrdering Ordering)
|
|
|
|
: SSID(SSID), Ordering(Ordering) {}
|
2017-07-22 05:19:23 +08:00
|
|
|
|
2017-09-06 03:01:10 +08:00
|
|
|
SIMemOpInfo(SyncScope::ID SSID, AtomicOrdering Ordering,
|
2017-09-08 01:14:54 +08:00
|
|
|
AtomicOrdering FailureOrdering, bool IsNonTemporal = false)
|
|
|
|
: SSID(SSID), Ordering(Ordering), FailureOrdering(FailureOrdering),
|
|
|
|
IsNonTemporal(IsNonTemporal) {}
|
2017-09-06 00:41:25 +08:00
|
|
|
|
2017-09-08 00:14:21 +08:00
|
|
|
/// \returns Info constructed from \p MI, which has at least machine memory
|
|
|
|
/// operand.
|
|
|
|
static Optional<SIMemOpInfo> constructFromMIWithMMO(
|
|
|
|
const MachineBasicBlock::iterator &MI);
|
|
|
|
|
2017-09-06 00:41:25 +08:00
|
|
|
public:
|
|
|
|
/// \returns Synchronization scope ID of the machine instruction used to
|
|
|
|
/// create this SIMemOpInfo.
|
|
|
|
SyncScope::ID getSSID() const {
|
|
|
|
return SSID;
|
|
|
|
}
|
|
|
|
/// \returns Ordering constraint of the machine instruction used to
|
|
|
|
/// create this SIMemOpInfo.
|
|
|
|
AtomicOrdering getOrdering() const {
|
|
|
|
return Ordering;
|
|
|
|
}
|
|
|
|
/// \returns Failure ordering constraint of the machine instruction used to
|
|
|
|
/// create this SIMemOpInfo.
|
|
|
|
AtomicOrdering getFailureOrdering() const {
|
|
|
|
return FailureOrdering;
|
|
|
|
}
|
2017-09-08 01:14:54 +08:00
|
|
|
/// \returns True if memory access of the machine instruction used to
|
|
|
|
/// create this SIMemOpInfo is non-temporal, false otherwise.
|
|
|
|
bool isNonTemporal() const {
|
|
|
|
return IsNonTemporal;
|
|
|
|
}
|
2017-09-06 00:41:25 +08:00
|
|
|
|
2017-09-06 03:01:10 +08:00
|
|
|
/// \returns True if ordering constraint of the machine instruction used to
|
|
|
|
/// create this SIMemOpInfo is unordered or higher, false otherwise.
|
|
|
|
bool isAtomic() const {
|
|
|
|
return Ordering != AtomicOrdering::NotAtomic;
|
|
|
|
}
|
|
|
|
|
2017-09-06 00:41:25 +08:00
|
|
|
/// \returns Load info if \p MI is a load operation, "None" otherwise.
|
|
|
|
static Optional<SIMemOpInfo> getLoadInfo(
|
|
|
|
const MachineBasicBlock::iterator &MI);
|
|
|
|
/// \returns Store info if \p MI is a store operation, "None" otherwise.
|
|
|
|
static Optional<SIMemOpInfo> getStoreInfo(
|
|
|
|
const MachineBasicBlock::iterator &MI);
|
|
|
|
/// \returns Atomic fence info if \p MI is an atomic fence operation,
|
|
|
|
/// "None" otherwise.
|
|
|
|
static Optional<SIMemOpInfo> getAtomicFenceInfo(
|
|
|
|
const MachineBasicBlock::iterator &MI);
|
2018-02-07 03:11:56 +08:00
|
|
|
/// \returns Atomic cmpxchg/rmw info if \p MI is an atomic cmpxchg or
|
|
|
|
/// rmw operation, "None" otherwise.
|
2018-02-09 14:05:33 +08:00
|
|
|
static Optional<SIMemOpInfo> getAtomicCmpxchgOrRmwInfo(
|
2017-09-06 00:41:25 +08:00
|
|
|
const MachineBasicBlock::iterator &MI);
|
2017-09-08 00:14:21 +08:00
|
|
|
|
|
|
|
/// \brief Reports unknown synchronization scope used in \p MI to LLVM
|
|
|
|
/// context.
|
|
|
|
static void reportUnknownSyncScope(
|
|
|
|
const MachineBasicBlock::iterator &MI);
|
2017-09-06 00:18:05 +08:00
|
|
|
};
|
2017-07-22 05:19:23 +08:00
|
|
|
|
2017-09-06 00:18:05 +08:00
|
|
|
class SIMemoryLegalizer final : public MachineFunctionPass {
|
|
|
|
private:
|
2017-07-22 05:19:23 +08:00
|
|
|
/// \brief Machine module info.
|
|
|
|
const AMDGPUMachineModuleInfo *MMI = nullptr;
|
2017-08-10 08:46:15 +08:00
|
|
|
|
2017-07-22 05:19:23 +08:00
|
|
|
/// \brief Instruction info.
|
|
|
|
const SIInstrInfo *TII = nullptr;
|
|
|
|
|
|
|
|
/// \brief Immediate for "vmcnt(0)".
|
|
|
|
unsigned Vmcnt0Immediate = 0;
|
2017-08-10 08:46:15 +08:00
|
|
|
|
2017-07-22 05:19:23 +08:00
|
|
|
/// \brief Opcode for cache invalidation instruction (L1).
|
2018-02-09 14:05:33 +08:00
|
|
|
unsigned VmemSIMDCacheInvalidateOpc = 0;
|
2017-07-22 05:19:23 +08:00
|
|
|
|
|
|
|
/// \brief List of atomic pseudo instructions.
|
|
|
|
std::list<MachineBasicBlock::iterator> AtomicPseudoMIs;
|
|
|
|
|
2017-09-08 01:14:54 +08:00
|
|
|
/// \brief Sets named bit (BitName) to "true" if present in \p MI. Returns
|
|
|
|
/// true if \p MI is modified, false otherwise.
|
|
|
|
template <uint16_t BitName>
|
|
|
|
bool enableNamedBit(const MachineBasicBlock::iterator &MI) const {
|
|
|
|
int BitIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(), BitName);
|
|
|
|
if (BitIdx == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MachineOperand &Bit = MI->getOperand(BitIdx);
|
|
|
|
if (Bit.getImm() != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Bit.setImm(1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Sets GLC bit to "true" if present in \p MI. Returns true if \p MI
|
|
|
|
/// is modified, false otherwise.
|
|
|
|
bool enableGLCBit(const MachineBasicBlock::iterator &MI) const {
|
|
|
|
return enableNamedBit<AMDGPU::OpName::glc>(MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Sets SLC bit to "true" if present in \p MI. Returns true if \p MI
|
|
|
|
/// is modified, false otherwise.
|
|
|
|
bool enableSLCBit(const MachineBasicBlock::iterator &MI) const {
|
|
|
|
return enableNamedBit<AMDGPU::OpName::slc>(MI);
|
|
|
|
}
|
|
|
|
|
2017-07-22 05:19:23 +08:00
|
|
|
/// \brief Inserts "buffer_wbinvl1_vol" instruction \p Before or after \p MI.
|
|
|
|
/// Always returns true.
|
2018-02-09 14:05:33 +08:00
|
|
|
bool insertVmemSIMDCacheInvalidate(MachineBasicBlock::iterator &MI,
|
|
|
|
bool Before = true) const;
|
2017-07-22 05:19:23 +08:00
|
|
|
/// \brief Inserts "s_waitcnt vmcnt(0)" instruction \p Before or after \p MI.
|
|
|
|
/// Always returns true.
|
|
|
|
bool insertWaitcntVmcnt0(MachineBasicBlock::iterator &MI,
|
|
|
|
bool Before = true) const;
|
|
|
|
|
|
|
|
/// \brief Removes all processed atomic pseudo instructions from the current
|
|
|
|
/// function. Returns true if current function is modified, false otherwise.
|
|
|
|
bool removeAtomicPseudoMIs();
|
|
|
|
|
2017-08-19 01:30:02 +08:00
|
|
|
/// \brief Expands load operation \p MI. Returns true if instructions are
|
|
|
|
/// added/deleted or \p MI is modified, false otherwise.
|
2017-09-06 00:18:05 +08:00
|
|
|
bool expandLoad(const SIMemOpInfo &MOI,
|
|
|
|
MachineBasicBlock::iterator &MI);
|
2017-08-19 01:30:02 +08:00
|
|
|
/// \brief Expands store operation \p MI. Returns true if instructions are
|
|
|
|
/// added/deleted or \p MI is modified, false otherwise.
|
2017-09-06 00:18:05 +08:00
|
|
|
bool expandStore(const SIMemOpInfo &MOI,
|
|
|
|
MachineBasicBlock::iterator &MI);
|
2017-08-20 02:44:27 +08:00
|
|
|
/// \brief Expands atomic fence operation \p MI. Returns true if
|
|
|
|
/// instructions are added/deleted or \p MI is modified, false otherwise.
|
2017-09-06 00:18:05 +08:00
|
|
|
bool expandAtomicFence(const SIMemOpInfo &MOI,
|
2017-08-20 02:44:27 +08:00
|
|
|
MachineBasicBlock::iterator &MI);
|
2018-02-07 03:11:56 +08:00
|
|
|
/// \brief Expands atomic cmpxchg or rmw operation \p MI. Returns true if
|
2017-07-22 05:19:23 +08:00
|
|
|
/// instructions are added/deleted or \p MI is modified, false otherwise.
|
2018-02-09 14:05:33 +08:00
|
|
|
bool expandAtomicCmpxchgOrRmw(const SIMemOpInfo &MOI,
|
|
|
|
MachineBasicBlock::iterator &MI);
|
2017-07-22 05:19:23 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
2017-08-10 08:46:15 +08:00
|
|
|
SIMemoryLegalizer() : MachineFunctionPass(ID) {}
|
2017-07-22 05:19:23 +08:00
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef getPassName() const override {
|
|
|
|
return PASS_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace anonymous
|
|
|
|
|
2017-09-08 00:14:21 +08:00
|
|
|
/* static */
|
|
|
|
Optional<SIMemOpInfo> SIMemOpInfo::constructFromMIWithMMO(
|
|
|
|
const MachineBasicBlock::iterator &MI) {
|
|
|
|
assert(MI->getNumMemOperands() > 0);
|
|
|
|
|
|
|
|
const MachineFunction *MF = MI->getParent()->getParent();
|
|
|
|
const AMDGPUMachineModuleInfo *MMI =
|
|
|
|
&MF->getMMI().getObjFileInfo<AMDGPUMachineModuleInfo>();
|
|
|
|
|
|
|
|
SyncScope::ID SSID = SyncScope::SingleThread;
|
|
|
|
AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
|
|
|
|
AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic;
|
2017-09-08 01:14:54 +08:00
|
|
|
bool IsNonTemporal = true;
|
2017-09-08 00:14:21 +08:00
|
|
|
|
|
|
|
// Validator should check whether or not MMOs cover the entire set of
|
|
|
|
// locations accessed by the memory instruction.
|
|
|
|
for (const auto &MMO : MI->memoperands()) {
|
|
|
|
const auto &IsSyncScopeInclusion =
|
|
|
|
MMI->isSyncScopeInclusion(SSID, MMO->getSyncScopeID());
|
|
|
|
if (!IsSyncScopeInclusion) {
|
|
|
|
reportUnknownSyncScope(MI);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSID = IsSyncScopeInclusion.getValue() ? SSID : MMO->getSyncScopeID();
|
|
|
|
Ordering =
|
|
|
|
isStrongerThan(Ordering, MMO->getOrdering()) ?
|
|
|
|
Ordering : MMO->getOrdering();
|
|
|
|
FailureOrdering =
|
|
|
|
isStrongerThan(FailureOrdering, MMO->getFailureOrdering()) ?
|
|
|
|
FailureOrdering : MMO->getFailureOrdering();
|
2017-09-08 01:14:54 +08:00
|
|
|
|
|
|
|
if (!(MMO->getFlags() & MachineMemOperand::MONonTemporal))
|
|
|
|
IsNonTemporal = false;
|
2017-09-08 00:14:21 +08:00
|
|
|
}
|
|
|
|
|
2017-09-08 01:14:54 +08:00
|
|
|
return SIMemOpInfo(SSID, Ordering, FailureOrdering, IsNonTemporal);
|
2017-09-08 00:14:21 +08:00
|
|
|
}
|
|
|
|
|
2017-09-06 00:41:25 +08:00
|
|
|
/* static */
|
|
|
|
Optional<SIMemOpInfo> SIMemOpInfo::getLoadInfo(
|
2017-07-22 05:19:23 +08:00
|
|
|
const MachineBasicBlock::iterator &MI) {
|
|
|
|
assert(MI->getDesc().TSFlags & SIInstrFlags::maybeAtomic);
|
|
|
|
|
|
|
|
if (!(MI->mayLoad() && !MI->mayStore()))
|
|
|
|
return None;
|
2017-09-08 00:14:21 +08:00
|
|
|
|
|
|
|
// Be conservative if there are no memory operands.
|
|
|
|
if (MI->getNumMemOperands() == 0)
|
2017-09-06 03:01:10 +08:00
|
|
|
return SIMemOpInfo(SyncScope::System,
|
|
|
|
AtomicOrdering::SequentiallyConsistent);
|
2017-07-22 05:19:23 +08:00
|
|
|
|
2017-09-08 00:14:21 +08:00
|
|
|
return SIMemOpInfo::constructFromMIWithMMO(MI);
|
2017-07-22 05:19:23 +08:00
|
|
|
}
|
|
|
|
|
2017-09-06 00:41:25 +08:00
|
|
|
/* static */
|
|
|
|
Optional<SIMemOpInfo> SIMemOpInfo::getStoreInfo(
|
|
|
|
const MachineBasicBlock::iterator &MI) {
|
2017-07-22 05:19:23 +08:00
|
|
|
assert(MI->getDesc().TSFlags & SIInstrFlags::maybeAtomic);
|
|
|
|
|
|
|
|
if (!(!MI->mayLoad() && MI->mayStore()))
|
|
|
|
return None;
|
2017-09-08 00:14:21 +08:00
|
|
|
|
|
|
|
// Be conservative if there are no memory operands.
|
|
|
|
if (MI->getNumMemOperands() == 0)
|
2017-09-06 03:01:10 +08:00
|
|
|
return SIMemOpInfo(SyncScope::System,
|
|
|
|
AtomicOrdering::SequentiallyConsistent);
|
2017-07-22 05:19:23 +08:00
|
|
|
|
2017-09-08 00:14:21 +08:00
|
|
|
return SIMemOpInfo::constructFromMIWithMMO(MI);
|
2017-07-22 05:19:23 +08:00
|
|
|
}
|
|
|
|
|
2017-09-06 00:41:25 +08:00
|
|
|
/* static */
|
|
|
|
Optional<SIMemOpInfo> SIMemOpInfo::getAtomicFenceInfo(
|
|
|
|
const MachineBasicBlock::iterator &MI) {
|
2017-08-20 02:44:27 +08:00
|
|
|
assert(MI->getDesc().TSFlags & SIInstrFlags::maybeAtomic);
|
|
|
|
|
|
|
|
if (MI->getOpcode() != AMDGPU::ATOMIC_FENCE)
|
|
|
|
return None;
|
|
|
|
|
|
|
|
SyncScope::ID SSID =
|
|
|
|
static_cast<SyncScope::ID>(MI->getOperand(1).getImm());
|
|
|
|
AtomicOrdering Ordering =
|
|
|
|
static_cast<AtomicOrdering>(MI->getOperand(0).getImm());
|
2017-09-06 03:01:10 +08:00
|
|
|
return SIMemOpInfo(SSID, Ordering);
|
2017-08-20 02:44:27 +08:00
|
|
|
}
|
|
|
|
|
2017-09-06 00:41:25 +08:00
|
|
|
/* static */
|
2018-02-09 14:05:33 +08:00
|
|
|
Optional<SIMemOpInfo> SIMemOpInfo::getAtomicCmpxchgOrRmwInfo(
|
2017-09-06 00:41:25 +08:00
|
|
|
const MachineBasicBlock::iterator &MI) {
|
2017-07-22 05:19:23 +08:00
|
|
|
assert(MI->getDesc().TSFlags & SIInstrFlags::maybeAtomic);
|
|
|
|
|
|
|
|
if (!(MI->mayLoad() && MI->mayStore()))
|
|
|
|
return None;
|
2017-09-08 00:14:21 +08:00
|
|
|
|
|
|
|
// Be conservative if there are no memory operands.
|
|
|
|
if (MI->getNumMemOperands() == 0)
|
2017-09-06 03:01:10 +08:00
|
|
|
return SIMemOpInfo(SyncScope::System,
|
|
|
|
AtomicOrdering::SequentiallyConsistent,
|
|
|
|
AtomicOrdering::SequentiallyConsistent);
|
2017-07-22 05:19:23 +08:00
|
|
|
|
2017-09-08 00:14:21 +08:00
|
|
|
return SIMemOpInfo::constructFromMIWithMMO(MI);
|
2017-07-22 05:19:23 +08:00
|
|
|
}
|
|
|
|
|
2017-09-08 00:14:21 +08:00
|
|
|
/* static */
|
|
|
|
void SIMemOpInfo::reportUnknownSyncScope(
|
|
|
|
const MachineBasicBlock::iterator &MI) {
|
2017-12-16 06:22:58 +08:00
|
|
|
DiagnosticInfoUnsupported Diag(MI->getParent()->getParent()->getFunction(),
|
2017-09-08 00:14:21 +08:00
|
|
|
"Unsupported synchronization scope");
|
2017-12-16 06:22:58 +08:00
|
|
|
LLVMContext *CTX = &MI->getParent()->getParent()->getFunction().getContext();
|
2017-09-08 00:14:21 +08:00
|
|
|
CTX->diagnose(Diag);
|
2017-07-22 05:19:23 +08:00
|
|
|
}
|
|
|
|
|
2018-02-09 14:05:33 +08:00
|
|
|
bool SIMemoryLegalizer::insertVmemSIMDCacheInvalidate(
|
|
|
|
MachineBasicBlock::iterator &MI, bool Before) const {
|
2017-09-06 00:41:25 +08:00
|
|
|
MachineBasicBlock &MBB = *MI->getParent();
|
|
|
|
DebugLoc DL = MI->getDebugLoc();
|
|
|
|
|
|
|
|
if (!Before)
|
|
|
|
++MI;
|
|
|
|
|
2018-02-09 14:05:33 +08:00
|
|
|
BuildMI(MBB, MI, DL, TII->get(VmemSIMDCacheInvalidateOpc));
|
2017-09-06 00:41:25 +08:00
|
|
|
|
|
|
|
if (!Before)
|
|
|
|
--MI;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SIMemoryLegalizer::insertWaitcntVmcnt0(MachineBasicBlock::iterator &MI,
|
|
|
|
bool Before) const {
|
|
|
|
MachineBasicBlock &MBB = *MI->getParent();
|
|
|
|
DebugLoc DL = MI->getDebugLoc();
|
|
|
|
|
|
|
|
if (!Before)
|
|
|
|
++MI;
|
|
|
|
|
|
|
|
BuildMI(MBB, MI, DL, TII->get(AMDGPU::S_WAITCNT)).addImm(Vmcnt0Immediate);
|
|
|
|
|
|
|
|
if (!Before)
|
|
|
|
--MI;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SIMemoryLegalizer::removeAtomicPseudoMIs() {
|
|
|
|
if (AtomicPseudoMIs.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (auto &MI : AtomicPseudoMIs)
|
|
|
|
MI->eraseFromParent();
|
|
|
|
|
|
|
|
AtomicPseudoMIs.clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-06 00:18:05 +08:00
|
|
|
bool SIMemoryLegalizer::expandLoad(const SIMemOpInfo &MOI,
|
2017-08-20 02:44:27 +08:00
|
|
|
MachineBasicBlock::iterator &MI) {
|
|
|
|
assert(MI->mayLoad() && !MI->mayStore());
|
2017-07-22 05:19:23 +08:00
|
|
|
|
|
|
|
bool Changed = false;
|
2017-08-20 02:44:27 +08:00
|
|
|
|
2017-09-06 03:01:10 +08:00
|
|
|
if (MOI.isAtomic()) {
|
|
|
|
if (MOI.getSSID() == SyncScope::System ||
|
|
|
|
MOI.getSSID() == MMI->getAgentSSID()) {
|
2018-02-06 12:06:04 +08:00
|
|
|
if (MOI.getOrdering() == AtomicOrdering::Monotonic ||
|
|
|
|
MOI.getOrdering() == AtomicOrdering::Acquire ||
|
2017-09-06 03:01:10 +08:00
|
|
|
MOI.getOrdering() == AtomicOrdering::SequentiallyConsistent)
|
2017-09-08 01:14:54 +08:00
|
|
|
Changed |= enableGLCBit(MI);
|
2017-09-06 03:01:10 +08:00
|
|
|
|
|
|
|
if (MOI.getOrdering() == AtomicOrdering::SequentiallyConsistent)
|
|
|
|
Changed |= insertWaitcntVmcnt0(MI);
|
|
|
|
|
|
|
|
if (MOI.getOrdering() == AtomicOrdering::Acquire ||
|
|
|
|
MOI.getOrdering() == AtomicOrdering::SequentiallyConsistent) {
|
|
|
|
Changed |= insertWaitcntVmcnt0(MI, false);
|
2018-02-09 14:05:33 +08:00
|
|
|
Changed |= insertVmemSIMDCacheInvalidate(MI, false);
|
2017-09-06 03:01:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
2017-09-08 00:14:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (MOI.getSSID() == SyncScope::SingleThread ||
|
|
|
|
MOI.getSSID() == MMI->getWorkgroupSSID() ||
|
|
|
|
MOI.getSSID() == MMI->getWavefrontSSID()) {
|
2017-09-06 03:01:10 +08:00
|
|
|
return Changed;
|
2017-08-20 02:44:27 +08:00
|
|
|
}
|
2017-09-08 00:14:21 +08:00
|
|
|
|
|
|
|
llvm_unreachable("Unsupported synchronization scope");
|
2017-07-22 05:19:23 +08:00
|
|
|
}
|
2017-09-06 03:01:10 +08:00
|
|
|
|
2017-09-08 01:14:54 +08:00
|
|
|
// Atomic instructions do not have the nontemporal attribute.
|
|
|
|
if (MOI.isNonTemporal()) {
|
|
|
|
Changed |= enableGLCBit(MI);
|
|
|
|
Changed |= enableSLCBit(MI);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-09-06 03:01:10 +08:00
|
|
|
return Changed;
|
2017-07-22 05:19:23 +08:00
|
|
|
}
|
|
|
|
|
2017-09-06 00:18:05 +08:00
|
|
|
bool SIMemoryLegalizer::expandStore(const SIMemOpInfo &MOI,
|
2017-08-20 02:44:27 +08:00
|
|
|
MachineBasicBlock::iterator &MI) {
|
|
|
|
assert(!MI->mayLoad() && MI->mayStore());
|
2017-07-22 05:19:23 +08:00
|
|
|
|
|
|
|
bool Changed = false;
|
2017-09-06 03:01:10 +08:00
|
|
|
|
|
|
|
if (MOI.isAtomic()) {
|
|
|
|
if (MOI.getSSID() == SyncScope::System ||
|
|
|
|
MOI.getSSID() == MMI->getAgentSSID()) {
|
|
|
|
if (MOI.getOrdering() == AtomicOrdering::Release ||
|
|
|
|
MOI.getOrdering() == AtomicOrdering::SequentiallyConsistent)
|
|
|
|
Changed |= insertWaitcntVmcnt0(MI);
|
|
|
|
|
|
|
|
return Changed;
|
2017-09-08 00:14:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (MOI.getSSID() == SyncScope::SingleThread ||
|
|
|
|
MOI.getSSID() == MMI->getWorkgroupSSID() ||
|
|
|
|
MOI.getSSID() == MMI->getWavefrontSSID()) {
|
2017-09-06 03:01:10 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
2017-09-08 00:14:21 +08:00
|
|
|
|
|
|
|
llvm_unreachable("Unsupported synchronization scope");
|
2017-07-22 05:19:23 +08:00
|
|
|
}
|
2017-09-06 03:01:10 +08:00
|
|
|
|
2017-09-08 01:14:54 +08:00
|
|
|
// Atomic instructions do not have the nontemporal attribute.
|
|
|
|
if (MOI.isNonTemporal()) {
|
|
|
|
Changed |= enableGLCBit(MI);
|
|
|
|
Changed |= enableSLCBit(MI);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-09-06 03:01:10 +08:00
|
|
|
return Changed;
|
2017-07-22 05:19:23 +08:00
|
|
|
}
|
|
|
|
|
2017-09-06 00:18:05 +08:00
|
|
|
bool SIMemoryLegalizer::expandAtomicFence(const SIMemOpInfo &MOI,
|
2017-08-20 02:44:27 +08:00
|
|
|
MachineBasicBlock::iterator &MI) {
|
|
|
|
assert(MI->getOpcode() == AMDGPU::ATOMIC_FENCE);
|
2017-07-22 05:19:23 +08:00
|
|
|
|
|
|
|
bool Changed = false;
|
2017-09-06 03:01:10 +08:00
|
|
|
|
|
|
|
if (MOI.isAtomic()) {
|
|
|
|
if (MOI.getSSID() == SyncScope::System ||
|
|
|
|
MOI.getSSID() == MMI->getAgentSSID()) {
|
|
|
|
if (MOI.getOrdering() == AtomicOrdering::Acquire ||
|
|
|
|
MOI.getOrdering() == AtomicOrdering::Release ||
|
|
|
|
MOI.getOrdering() == AtomicOrdering::AcquireRelease ||
|
|
|
|
MOI.getOrdering() == AtomicOrdering::SequentiallyConsistent)
|
|
|
|
Changed |= insertWaitcntVmcnt0(MI);
|
|
|
|
|
|
|
|
if (MOI.getOrdering() == AtomicOrdering::Acquire ||
|
|
|
|
MOI.getOrdering() == AtomicOrdering::AcquireRelease ||
|
|
|
|
MOI.getOrdering() == AtomicOrdering::SequentiallyConsistent)
|
2018-02-09 14:05:33 +08:00
|
|
|
Changed |= insertVmemSIMDCacheInvalidate(MI);
|
2017-09-06 03:01:10 +08:00
|
|
|
|
|
|
|
AtomicPseudoMIs.push_back(MI);
|
|
|
|
return Changed;
|
2017-09-08 00:14:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (MOI.getSSID() == SyncScope::SingleThread ||
|
|
|
|
MOI.getSSID() == MMI->getWorkgroupSSID() ||
|
|
|
|
MOI.getSSID() == MMI->getWavefrontSSID()) {
|
2017-09-06 03:01:10 +08:00
|
|
|
AtomicPseudoMIs.push_back(MI);
|
|
|
|
return Changed;
|
|
|
|
}
|
2017-09-08 00:14:21 +08:00
|
|
|
|
|
|
|
SIMemOpInfo::reportUnknownSyncScope(MI);
|
2017-07-22 05:19:23 +08:00
|
|
|
}
|
2017-09-06 03:01:10 +08:00
|
|
|
|
|
|
|
return Changed;
|
2017-07-22 05:19:23 +08:00
|
|
|
}
|
|
|
|
|
2018-02-09 14:05:33 +08:00
|
|
|
bool SIMemoryLegalizer::expandAtomicCmpxchgOrRmw(const SIMemOpInfo &MOI,
|
|
|
|
MachineBasicBlock::iterator &MI) {
|
2017-07-22 05:19:23 +08:00
|
|
|
assert(MI->mayLoad() && MI->mayStore());
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
2017-09-06 03:01:10 +08:00
|
|
|
if (MOI.isAtomic()) {
|
|
|
|
if (MOI.getSSID() == SyncScope::System ||
|
|
|
|
MOI.getSSID() == MMI->getAgentSSID()) {
|
|
|
|
if (MOI.getOrdering() == AtomicOrdering::Release ||
|
|
|
|
MOI.getOrdering() == AtomicOrdering::AcquireRelease ||
|
|
|
|
MOI.getOrdering() == AtomicOrdering::SequentiallyConsistent ||
|
|
|
|
MOI.getFailureOrdering() == AtomicOrdering::SequentiallyConsistent)
|
|
|
|
Changed |= insertWaitcntVmcnt0(MI);
|
|
|
|
|
|
|
|
if (MOI.getOrdering() == AtomicOrdering::Acquire ||
|
|
|
|
MOI.getOrdering() == AtomicOrdering::AcquireRelease ||
|
|
|
|
MOI.getOrdering() == AtomicOrdering::SequentiallyConsistent ||
|
|
|
|
MOI.getFailureOrdering() == AtomicOrdering::Acquire ||
|
|
|
|
MOI.getFailureOrdering() == AtomicOrdering::SequentiallyConsistent) {
|
|
|
|
Changed |= insertWaitcntVmcnt0(MI, false);
|
2018-02-09 14:05:33 +08:00
|
|
|
Changed |= insertVmemSIMDCacheInvalidate(MI, false);
|
2017-09-06 03:01:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
2017-09-08 00:14:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (MOI.getSSID() == SyncScope::SingleThread ||
|
|
|
|
MOI.getSSID() == MMI->getWorkgroupSSID() ||
|
|
|
|
MOI.getSSID() == MMI->getWavefrontSSID()) {
|
2017-09-08 01:14:54 +08:00
|
|
|
Changed |= enableGLCBit(MI);
|
2017-09-06 03:01:10 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
2017-09-08 00:14:21 +08:00
|
|
|
|
|
|
|
llvm_unreachable("Unsupported synchronization scope");
|
2017-07-22 05:19:23 +08:00
|
|
|
}
|
2017-09-06 03:01:10 +08:00
|
|
|
|
|
|
|
return Changed;
|
2017-07-22 05:19:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SIMemoryLegalizer::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
bool Changed = false;
|
|
|
|
const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
|
|
|
|
const IsaInfo::IsaVersion IV = IsaInfo::getIsaVersion(ST.getFeatureBits());
|
|
|
|
|
|
|
|
MMI = &MF.getMMI().getObjFileInfo<AMDGPUMachineModuleInfo>();
|
|
|
|
TII = ST.getInstrInfo();
|
|
|
|
|
|
|
|
Vmcnt0Immediate =
|
|
|
|
AMDGPU::encodeWaitcnt(IV, 0, getExpcntBitMask(IV), getLgkmcntBitMask(IV));
|
2018-02-09 14:05:33 +08:00
|
|
|
VmemSIMDCacheInvalidateOpc =
|
|
|
|
ST.getGeneration() <= AMDGPUSubtarget::SOUTHERN_ISLANDS ?
|
|
|
|
AMDGPU::BUFFER_WBINVL1 : AMDGPU::BUFFER_WBINVL1_VOL;
|
2017-07-22 05:19:23 +08:00
|
|
|
|
|
|
|
for (auto &MBB : MF) {
|
|
|
|
for (auto MI = MBB.begin(); MI != MBB.end(); ++MI) {
|
|
|
|
if (!(MI->getDesc().TSFlags & SIInstrFlags::maybeAtomic))
|
|
|
|
continue;
|
|
|
|
|
2017-09-06 00:41:25 +08:00
|
|
|
if (const auto &MOI = SIMemOpInfo::getLoadInfo(MI))
|
2017-08-19 01:30:02 +08:00
|
|
|
Changed |= expandLoad(MOI.getValue(), MI);
|
2017-09-06 00:41:25 +08:00
|
|
|
else if (const auto &MOI = SIMemOpInfo::getStoreInfo(MI))
|
2017-08-19 01:30:02 +08:00
|
|
|
Changed |= expandStore(MOI.getValue(), MI);
|
2017-09-06 00:41:25 +08:00
|
|
|
else if (const auto &MOI = SIMemOpInfo::getAtomicFenceInfo(MI))
|
2017-08-20 02:44:27 +08:00
|
|
|
Changed |= expandAtomicFence(MOI.getValue(), MI);
|
2018-02-09 14:05:33 +08:00
|
|
|
else if (const auto &MOI = SIMemOpInfo::getAtomicCmpxchgOrRmwInfo(MI))
|
|
|
|
Changed |= expandAtomicCmpxchgOrRmw(MOI.getValue(), MI);
|
2017-07-22 05:19:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Changed |= removeAtomicPseudoMIs();
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
INITIALIZE_PASS(SIMemoryLegalizer, DEBUG_TYPE, PASS_NAME, false, false)
|
|
|
|
|
|
|
|
char SIMemoryLegalizer::ID = 0;
|
|
|
|
char &llvm::SIMemoryLegalizerID = SIMemoryLegalizer::ID;
|
|
|
|
|
|
|
|
FunctionPass *llvm::createSIMemoryLegalizerPass() {
|
|
|
|
return new SIMemoryLegalizer();
|
|
|
|
}
|