Skip to content

Commit

Permalink
Revert "[NVPTX] instcombine known pointer AS checks." (#114319)
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem-B authored Oct 30, 2024
1 parent d043670 commit 04e876e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 372 deletions.
33 changes: 0 additions & 33 deletions llvm/include/llvm/Support/NVPTXAddrSpace.h

This file was deleted.

12 changes: 10 additions & 2 deletions llvm/lib/Target/NVPTX/MCTargetDesc/NVPTXBaseInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@
#ifndef LLVM_LIB_TARGET_NVPTX_MCTARGETDESC_NVPTXBASEINFO_H
#define LLVM_LIB_TARGET_NVPTX_MCTARGETDESC_NVPTXBASEINFO_H

#include "llvm/Support/NVPTXAddrSpace.h"
namespace llvm {

using namespace NVPTXAS;
enum AddressSpace {
ADDRESS_SPACE_GENERIC = 0,
ADDRESS_SPACE_GLOBAL = 1,
ADDRESS_SPACE_SHARED = 3,
ADDRESS_SPACE_CONST = 4,
ADDRESS_SPACE_LOCAL = 5,

// NVVM Internal
ADDRESS_SPACE_PARAM = 101
};

namespace NVPTXII {
enum {
Expand Down
63 changes: 3 additions & 60 deletions llvm/lib/Target/NVPTX/NVPTXTargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
#include "llvm/CodeGen/CostTable.h"
#include "llvm/CodeGen/TargetLowering.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/IntrinsicsNVPTX.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Transforms/InstCombine/InstCombiner.h"
#include <optional>
using namespace llvm;
Expand Down Expand Up @@ -119,8 +117,7 @@ bool NVPTXTTIImpl::isSourceOfDivergence(const Value *V) {
}

// Convert NVVM intrinsics to target-generic LLVM code where possible.
static Instruction *convertNvvmIntrinsicToLlvm(InstCombiner &IC,
IntrinsicInst *II) {
static Instruction *simplifyNvvmIntrinsic(IntrinsicInst *II, InstCombiner &IC) {
// Each NVVM intrinsic we can simplify can be replaced with one of:
//
// * an LLVM intrinsic,
Expand Down Expand Up @@ -416,65 +413,11 @@ static Instruction *convertNvvmIntrinsicToLlvm(InstCombiner &IC,
llvm_unreachable("All SpecialCase enumerators should be handled in switch.");
}

// Returns an instruction pointer (may be nullptr if we do not know the answer).
// Returns nullopt if `II` is not one of the `isspacep` intrinsics.
static std::optional<Instruction *>
handleSpaceCheckIntrinsics(InstCombiner &IC, IntrinsicInst &II) {
Value *Op0 = II.getArgOperand(0);
// Returns true/false when we know the answer, nullopt otherwise.
auto CheckASMatch = [](unsigned IID, unsigned AS) -> std::optional<bool> {
if (AS == NVPTXAS::ADDRESS_SPACE_GENERIC ||
AS == NVPTXAS::ADDRESS_SPACE_PARAM)
return std::nullopt; // Got to check at run-time.
switch (IID) {
case Intrinsic::nvvm_isspacep_global:
return AS == NVPTXAS::ADDRESS_SPACE_GLOBAL;
case Intrinsic::nvvm_isspacep_local:
return AS == NVPTXAS::ADDRESS_SPACE_LOCAL;
case Intrinsic::nvvm_isspacep_shared:
return AS == NVPTXAS::ADDRESS_SPACE_SHARED;
case Intrinsic::nvvm_isspacep_shared_cluster:
// We can't tell shared from shared_cluster at compile time from AS alone,
// but it can't be either is AS is not shared.
return AS == NVPTXAS::ADDRESS_SPACE_SHARED ? std::nullopt
: std::optional{false};
case Intrinsic::nvvm_isspacep_const:
return AS == NVPTXAS::ADDRESS_SPACE_CONST;
default:
llvm_unreachable("Unexpected intrinsic");
}
};

switch (auto IID = II.getIntrinsicID()) {
case Intrinsic::nvvm_isspacep_global:
case Intrinsic::nvvm_isspacep_local:
case Intrinsic::nvvm_isspacep_shared:
case Intrinsic::nvvm_isspacep_shared_cluster:
case Intrinsic::nvvm_isspacep_const: {
auto *Ty = II.getType();
unsigned AS = Op0->getType()->getPointerAddressSpace();
// Peek through ASC to generic AS.
// TODO: we could dig deeper through both ASCs and GEPs.
if (AS == NVPTXAS::ADDRESS_SPACE_GENERIC)
if (auto *ASCO = dyn_cast<AddrSpaceCastOperator>(Op0))
AS = ASCO->getOperand(0)->getType()->getPointerAddressSpace();

if (std::optional<bool> Answer = CheckASMatch(IID, AS))
return IC.replaceInstUsesWith(II, ConstantInt::get(Ty, *Answer));
return nullptr; // Don't know the answer, got to check at run time.
}
default:
return std::nullopt;
}
}

std::optional<Instruction *>
NVPTXTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
if (std::optional<Instruction *> I = handleSpaceCheckIntrinsics(IC, II))
return *I;
if (Instruction *I = convertNvvmIntrinsicToLlvm(IC, &II))
if (Instruction *I = simplifyNvvmIntrinsic(&II, IC)) {
return I;

}
return std::nullopt;
}

Expand Down
Loading

0 comments on commit 04e876e

Please sign in to comment.