Skip to content

[SandboxIR] Implement PossiblyNonNegInst #106149

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 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class CatchReturnInst;
class CleanupReturnInst;
class GetElementPtrInst;
class CastInst;
class PossiblyNonNegInst;
class PtrToIntInst;
class BitCastInst;
class AllocaInst;
Expand Down Expand Up @@ -2762,6 +2763,28 @@ class CastInst : public UnaryInstruction {
Type *getDestTy() const { return cast<llvm::CastInst>(Val)->getDestTy(); }
};

/// Instruction that can have a nneg flag (zext/uitofp).
class PossiblyNonNegInst : public CastInst {
public:
bool hasNonNeg() const {
return cast<llvm::PossiblyNonNegInst>(Val)->hasNonNeg();
}
void setNonNeg(bool B);
/// For isa/dyn_cast.
static bool classof(const Value *From) {
if (auto *I = dyn_cast<Instruction>(From)) {
switch (I->getOpcode()) {
case Opcode::ZExt:
case Opcode::UIToFP:
return true;
default:
return false;
}
}
return false;
}
};

// Helper class to simplify stamping out CastInst subclasses.
template <Instruction::Opcode Op> class CastInstImpl : public CastInst {
public:
Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/SandboxIR/SandboxIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1991,6 +1991,13 @@ bool CastInst::classof(const Value *From) {
return From->getSubclassID() == ClassID::Cast;
}

void PossiblyNonNegInst::setNonNeg(bool B) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&PossiblyNonNegInst::hasNonNeg,
&PossiblyNonNegInst::setNonNeg>>(this);
cast<llvm::PossiblyNonNegInst>(Val)->setNonNeg(B);
}

Value *InsertElementInst::create(Value *Vec, Value *NewElt, Value *Idx,
Instruction *InsertBefore, Context &Ctx,
const Twine &Name) {
Expand Down
42 changes: 42 additions & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3682,6 +3682,48 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
}
}

TEST_F(SandboxIRTest, PossiblyNonNegInst) {
parseIR(C, R"IR(
define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
%zext = zext i32 %arg to i64
%uitofp = uitofp i32 %arg to float

%sext = sext i32 %arg to i64
%fptoui = fptoui float %farg to i32
%fptosi = fptosi float %farg to i32
%fpext = fpext float %farg to double
%ptrtoint = ptrtoint ptr %ptr to i32
%inttoptr = inttoptr i32 %arg to ptr
%sitofp = sitofp i32 %arg to float
%trunc = trunc i32 %arg to i16
%fptrunc = fptrunc double %darg to float
%bitcast = bitcast i32 %arg to float
%addrspacecast = addrspacecast ptr %ptr to ptr addrspace(1)
ret void
}
)IR");
Function &LLVMF = *M->getFunction("foo");
sandboxir::Context Ctx(C);
sandboxir::Function *F = Ctx.createFunction(&LLVMF);
auto *BB = &*F->begin();
auto It = BB->begin();
auto *PNNI0 = cast<sandboxir::PossiblyNonNegInst>(&*It++);
auto *PNNI1 = cast<sandboxir::PossiblyNonNegInst>(&*It++);
for (auto ItE = BB->end(); It != ItE; ++It)
EXPECT_FALSE(isa<sandboxir::PossiblyNonNegInst>(&*It++));

for (auto *PNNI : {PNNI0, PNNI1}) {
// Check setNonNeg(), hasNonNeg().
auto OrigNonNeg = PNNI->hasNonNeg();
auto NewNonNeg = true;
EXPECT_NE(NewNonNeg, OrigNonNeg);
PNNI->setNonNeg(NewNonNeg);
EXPECT_EQ(PNNI->hasNonNeg(), NewNonNeg);
PNNI->setNonNeg(OrigNonNeg);
EXPECT_EQ(PNNI->hasNonNeg(), OrigNonNeg);
}
}

/// CastInst's subclasses are very similar so we can use a common test function
/// for them.
template <typename SubclassT, sandboxir::Instruction::Opcode OpcodeT>
Expand Down
26 changes: 26 additions & 0 deletions llvm/unittests/SandboxIR/TrackerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,32 @@ define void @foo(i8 %arg0, i8 %arg1) {
EXPECT_EQ(PDI->isDisjoint(), OrigIsDisjoint);
}

TEST_F(TrackerTest, PossiblyNonNegInstSetters) {
parseIR(C, R"IR(
define void @foo(i32 %arg) {
%zext = zext i32 %arg to i64
ret void
}
)IR");
Function &LLVMF = *M->getFunction("foo");
sandboxir::Context Ctx(C);

auto &F = *Ctx.createFunction(&LLVMF);
auto *BB = &*F.begin();
auto It = BB->begin();
auto *PNNI = cast<sandboxir::PossiblyNonNegInst>(&*It++);

// Check setNonNeg().
auto OrigNonNeg = PNNI->hasNonNeg();
auto NewNonNeg = true;
EXPECT_NE(NewNonNeg, OrigNonNeg);
Ctx.save();
PNNI->setNonNeg(NewNonNeg);
EXPECT_EQ(PNNI->hasNonNeg(), NewNonNeg);
Ctx.revert();
EXPECT_EQ(PNNI->hasNonNeg(), OrigNonNeg);
}

TEST_F(TrackerTest, AtomicRMWSetters) {
parseIR(C, R"IR(
define void @foo(ptr %ptr, i8 %arg) {
Expand Down
Loading