Skip to content

Commit ddbf5ea

Browse files
authored
[SandboxIR][NFC] Add some comments (#99359)
1 parent 321a0c0 commit ddbf5ea

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class Instruction;
7676
class User;
7777
class Value;
7878

79-
/// Returns the operand edge when dereferenced.
79+
/// Iterator for the `Use` edges of a User's operands.
80+
/// \Returns the operand `Use` when dereferenced.
8081
class OperandUseIterator {
8182
sandboxir::Use Use;
8283
/// Don't let the user create a non-empty OperandUseIterator.
@@ -103,7 +104,8 @@ class OperandUseIterator {
103104
}
104105
};
105106

106-
/// Returns user edge when dereferenced.
107+
/// Iterator for the `Use` edges of a Value's users.
108+
/// \Returns a `Use` when dereferenced.
107109
class UserUseIterator {
108110
sandboxir::Use Use;
109111
/// Don't let the user create a non-empty UserUseIterator.
@@ -162,7 +164,9 @@ class Value {
162164
unsigned UID;
163165
#endif
164166
/// The LLVM Value that corresponds to this SandboxIR Value.
165-
/// NOTE: Some SBInstructions, like Packs, may include more than one value.
167+
/// NOTE: Some sandboxir Instructions, like Packs, may include more than one
168+
/// value and in these cases `Val` points to the last instruction in program
169+
/// order.
166170
llvm::Value *Val = nullptr;
167171

168172
friend class Context; // For getting `Val`.
@@ -300,6 +304,7 @@ class Argument : public sandboxir::Value {
300304
#endif
301305
};
302306

307+
/// A sandboxir::User has operands.
303308
class User : public Value {
304309
protected:
305310
User(ClassID ID, llvm::Value *V, Context &Ctx) : Value(ID, V, Ctx) {}
@@ -309,6 +314,9 @@ class User : public Value {
309314
/// match the underlying LLVM instruction. All others should use a different
310315
/// implementation.
311316
Use getOperandUseDefault(unsigned OpIdx, bool Verify) const;
317+
/// \Returns the Use for the \p OpIdx'th operand. This is virtual to allow
318+
/// instructions to deviate from the LLVM IR operands, which is a requirement
319+
/// for sandboxir Instructions that consist of more than one LLVM Instruction.
312320
virtual Use getOperandUseInternal(unsigned OpIdx, bool Verify) const = 0;
313321
friend class OperandUseIterator; // for getOperandUseInternal()
314322

@@ -414,7 +422,8 @@ class Constant : public sandboxir::User {
414422
#endif
415423
};
416424

417-
/// The BasicBlock::iterator.
425+
/// Iterator for `Instruction`s in a `BasicBlock.
426+
/// \Returns an sandboxir::Instruction & when derereferenced.
418427
class BBIterator {
419428
public:
420429
using difference_type = std::ptrdiff_t;
@@ -456,7 +465,8 @@ class BBIterator {
456465
pointer get() const { return getInstr(It); }
457466
};
458467

459-
/// A sandboxir::User with operands and opcode.
468+
/// A sandboxir::User with operands, opcode and linked with previous/next
469+
/// instructions in an instruction list.
460470
class Instruction : public sandboxir::User {
461471
public:
462472
enum class Opcode {
@@ -577,6 +587,7 @@ class OpaqueInst : public sandboxir::Instruction {
577587
#endif
578588
};
579589

590+
/// Contains a list of sandboxir::Instruction's.
580591
class BasicBlock : public Value {
581592
/// Builds a graph that contains all values in \p BB in their original form
582593
/// i.e., no vectorization is taking place here.
@@ -643,9 +654,10 @@ class Context {
643654
friend void Instruction::eraseFromParent(); // For detach().
644655
/// Take ownership of VPtr and store it in `LLVMValueToValueMap`.
645656
Value *registerValue(std::unique_ptr<Value> &&VPtr);
646-
657+
/// This is the actual function that creates sandboxir values for \p V,
658+
/// and among others handles all instruction types.
647659
Value *getOrCreateValueInternal(llvm::Value *V, llvm::User *U = nullptr);
648-
660+
/// Get or create a sandboxir::Argument for an existing LLVM IR \p LLVMArg.
649661
Argument *getOrCreateArgument(llvm::Argument *LLVMArg) {
650662
auto Pair = LLVMValueToValueMap.insert({LLVMArg, nullptr});
651663
auto It = Pair.first;
@@ -655,11 +667,12 @@ class Context {
655667
}
656668
return cast<Argument>(It->second.get());
657669
}
658-
670+
/// Get or create a sandboxir::Value for an existing LLVM IR \p LLVMV.
659671
Value *getOrCreateValue(llvm::Value *LLVMV) {
660672
return getOrCreateValueInternal(LLVMV, 0);
661673
}
662-
674+
/// Create a sandboxir::BasicBlock for an existing LLVM IR \p BB. This will
675+
/// also create all contents of the block.
663676
BasicBlock *createBasicBlock(llvm::BasicBlock *BB);
664677

665678
friend class BasicBlock; // For getOrCreateValue().
@@ -671,7 +684,9 @@ class Context {
671684
const sandboxir::Value *getValue(const llvm::Value *V) const {
672685
return getValue(const_cast<llvm::Value *>(V));
673686
}
674-
687+
/// Create a sandboxir::Function for an existing LLVM IR \p F, including all
688+
/// blocks and instructions.
689+
/// This is the main API function for creating Sandbox IR.
675690
Function *createFunction(llvm::Function *F);
676691

677692
/// \Returns the number of values registered with Context.

llvm/lib/SandboxIR/SandboxIR.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ OperandUseIterator &OperandUseIterator::operator++() {
6060
}
6161

6262
UserUseIterator &UserUseIterator::operator++() {
63+
// Get the corresponding llvm::Use, get the next in the list, and update the
64+
// sandboxir::Use.
6365
llvm::Use *&LLVMUse = Use.LLVMUse;
6466
assert(LLVMUse != nullptr && "Already at end!");
6567
LLVMUse = LLVMUse->getNext();
@@ -107,6 +109,7 @@ void Value::replaceUsesWithIf(
107109
Value *OtherV, llvm::function_ref<bool(const Use &)> ShouldReplace) {
108110
assert(getType() == OtherV->getType() && "Can't replace with different type");
109111
llvm::Value *OtherVal = OtherV->Val;
112+
// We are delegating RUWIf to LLVM IR's RUWIf.
110113
Val->replaceUsesWithIf(
111114
OtherVal, [&ShouldReplace, this](llvm::Use &LLVMUse) -> bool {
112115
User *DstU = cast_or_null<User>(Ctx.getValue(LLVMUse.getUser()));
@@ -119,6 +122,7 @@ void Value::replaceUsesWithIf(
119122
void Value::replaceAllUsesWith(Value *Other) {
120123
assert(getType() == Other->getType() &&
121124
"Replacing with Value of different type!");
125+
// We are delegating RAUW to LLVM IR's RAUW.
122126
Val->replaceAllUsesWith(Other->Val);
123127
}
124128

@@ -208,10 +212,12 @@ bool User::classof(const Value *From) {
208212

209213
void User::setOperand(unsigned OperandIdx, Value *Operand) {
210214
assert(isa<llvm::User>(Val) && "No operands!");
215+
// We are delegating to llvm::User::setOperand().
211216
cast<llvm::User>(Val)->setOperand(OperandIdx, Operand->Val);
212217
}
213218

214219
bool User::replaceUsesOfWith(Value *FromV, Value *ToV) {
220+
// We are delegating RUOW to LLVM IR's RUOW.
215221
return cast<llvm::User>(Val)->replaceUsesOfWith(FromV->Val, ToV->Val);
216222
}
217223

@@ -282,6 +288,9 @@ BBIterator Instruction::getIterator() const {
282288
Instruction *Instruction::getNextNode() const {
283289
assert(getParent() != nullptr && "Detached!");
284290
assert(getIterator() != getParent()->end() && "Already at end!");
291+
// `Val` is the bottom-most LLVM IR instruction. Get the next in the chain,
292+
// and get the corresponding sandboxir Instruction that maps to it. This works
293+
// even for SandboxIR Instructions that map to more than one LLVM Instruction.
285294
auto *LLVMI = cast<llvm::Instruction>(Val);
286295
assert(LLVMI->getParent() != nullptr && "LLVM IR instr is detached!");
287296
auto *NextLLVMI = LLVMI->getNextNode();
@@ -342,6 +351,7 @@ void Instruction::insertBefore(Instruction *BeforeI) {
342351
assert(is_sorted(getLLVMInstrs(),
343352
[](auto *I1, auto *I2) { return I1->comesBefore(I2); }) &&
344353
"Expected program order!");
354+
// Insert the LLVM IR Instructions in program order.
345355
for (llvm::Instruction *I : getLLVMInstrs())
346356
I->insertBefore(BeforeTopI);
347357
}
@@ -362,11 +372,14 @@ void Instruction::insertInto(BasicBlock *BB, const BBIterator &WhereIt) {
362372
LLVMBeforeI = nullptr;
363373
LLVMBeforeIt = LLVMBB->end();
364374
}
375+
// Insert the LLVM IR Instructions in program order.
365376
for (llvm::Instruction *I : getLLVMInstrs())
366377
I->insertInto(LLVMBB, LLVMBeforeIt);
367378
}
368379

369380
BasicBlock *Instruction::getParent() const {
381+
// Get the LLVM IR Instruction that this maps to, get its parent, and get the
382+
// corresponding sandboxir::BasicBlock by looking it up in sandboxir::Context.
370383
auto *BB = cast<llvm::Instruction>(Val)->getParent();
371384
if (BB == nullptr)
372385
return nullptr;

0 commit comments

Comments
 (0)