-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
030015c
commit 3984aab
Showing
24 changed files
with
276 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,7 +1,73 @@ | ||
#pragma once | ||
|
||
#include <luisa/xir/instructions/branch.h> | ||
#include <luisa/xir/instructions/break.h> | ||
#include <luisa/xir/instructions/call.h> | ||
#include <luisa/xir/instructions/cast.h> | ||
#include <luisa/xir/instructions/comment.h> | ||
#include <luisa/xir/instructions/continue.h> | ||
#include <luisa/xir/instructions/gep.h> | ||
#include <luisa/xir/instructions/intrinsic.h> | ||
#include <luisa/xir/instructions/load.h> | ||
#include <luisa/xir/instructions/loop.h> | ||
#include <luisa/xir/instructions/phi.h> | ||
#include <luisa/xir/instructions/print.h> | ||
#include <luisa/xir/instructions/return.h> | ||
#include <luisa/xir/instructions/store.h> | ||
#include <luisa/xir/instructions/switch.h> | ||
#include <luisa/xir/instructions/unreachable.h> | ||
|
||
namespace luisa::compute::xir { | ||
|
||
class LC_XIR_API Builder { | ||
|
||
private: | ||
Instruction *_insertion_point = nullptr; | ||
|
||
private: | ||
void _check_valid_insertion_point() const noexcept; | ||
[[nodiscard]] Pool *_pool_from_insertion_point() const noexcept; | ||
|
||
template<typename T, typename... Args> | ||
[[nodiscard]] auto _create_and_append_instruction(Args &&...args) noexcept { | ||
auto inst = _pool_from_insertion_point()->create<T>(std::forward<Args>(args)...); | ||
_insertion_point->insert_after_self(inst); | ||
_insertion_point = inst; | ||
return inst; | ||
} | ||
|
||
public: | ||
Builder() noexcept; | ||
void set_insertion_point(Instruction *insertion_point) noexcept; | ||
void set_insertion_point(BasicBlock *block) noexcept; | ||
[[nodiscard]] auto insertion_point() noexcept -> Instruction * { return _insertion_point; } | ||
[[nodiscard]] auto insertion_point() const noexcept -> const Instruction * { return _insertion_point; } | ||
|
||
public: | ||
BranchInst *if_(Value *cond) noexcept; | ||
SwitchInst *switch_(Value *value) noexcept; | ||
LoopInst *loop() noexcept; | ||
|
||
BreakInst *break_() noexcept; | ||
ContinueInst *continue_() noexcept; | ||
UnreachableInst *unreachable_() noexcept; | ||
ReturnInst *return_(Value *value) noexcept; | ||
ReturnInst *return_void() noexcept; | ||
|
||
CallInst *call(const Type *type, Value *callee, luisa::span<Value *const> arguments) noexcept; | ||
IntrinsicInst *call(const Type *type, IntrinsicOp op, luisa::span<Value *const> arguments) noexcept; | ||
|
||
CastInst *static_cast_(const Type *type, Value *value) noexcept; | ||
CastInst *bitwise_cast_(const Type *type, Value *value) noexcept; | ||
|
||
PhiInst *phi(const Type *type) noexcept; | ||
PrintInst *print(luisa::string format, luisa::span<Value *const> values) noexcept; | ||
|
||
GEPInst *gep(const Type *type, Value *base, luisa::span<Value *const> indices) noexcept; | ||
LoadInst *load(const Type *type, Value *variable) noexcept; | ||
StoreInst *store(Value *variable, Value *value) noexcept; | ||
|
||
CommentInst *comment(luisa::string text) noexcept; | ||
}; | ||
|
||
} | ||
}// namespace luisa::compute::xir |
This file contains 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 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 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 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
set(LUISA_COMPUTE_XIR_SOURCES | ||
basic_block.cpp | ||
builder.cpp | ||
constant.cpp | ||
function.cpp | ||
instruction.cpp | ||
|
This file contains 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 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,98 @@ | ||
#include <luisa/core/logging.h> | ||
#include <luisa/xir/builder.h> | ||
|
||
namespace luisa::compute::xir { | ||
|
||
void Builder::_check_valid_insertion_point() const noexcept { | ||
LUISA_ASSERT(_insertion_point != nullptr, "Invalid insertion point."); | ||
} | ||
|
||
Pool *Builder::_pool_from_insertion_point() const noexcept { | ||
_check_valid_insertion_point(); | ||
return _insertion_point->pool(); | ||
} | ||
|
||
Builder::Builder() noexcept = default; | ||
|
||
BranchInst *Builder::if_(Value *cond) noexcept { | ||
return _create_and_append_instruction<BranchInst>(cond); | ||
} | ||
|
||
SwitchInst *Builder::switch_(Value *value) noexcept { | ||
return _create_and_append_instruction<SwitchInst>(value); | ||
} | ||
|
||
LoopInst *Builder::loop() noexcept { | ||
return _create_and_append_instruction<LoopInst>(); | ||
} | ||
|
||
BreakInst *Builder::break_() noexcept { | ||
return _create_and_append_instruction<BreakInst>(); | ||
} | ||
|
||
ContinueInst *Builder::continue_() noexcept { | ||
return _create_and_append_instruction<ContinueInst>(); | ||
} | ||
|
||
UnreachableInst *Builder::unreachable_() noexcept { | ||
return _create_and_append_instruction<UnreachableInst>(); | ||
} | ||
|
||
ReturnInst *Builder::return_(Value *value) noexcept { | ||
return _create_and_append_instruction<ReturnInst>(value); | ||
} | ||
|
||
ReturnInst *Builder::return_void() noexcept { | ||
return _create_and_append_instruction<ReturnInst>(); | ||
} | ||
|
||
CallInst *Builder::call(const Type *type, Value *callee, luisa::span<Value *const> arguments) noexcept { | ||
return _create_and_append_instruction<CallInst>(type, callee, arguments); | ||
} | ||
|
||
IntrinsicInst *Builder::call(const Type *type, IntrinsicOp op, luisa::span<Value *const> arguments) noexcept { | ||
return _create_and_append_instruction<IntrinsicInst>(type, op, arguments); | ||
} | ||
|
||
CastInst *Builder::static_cast_(const Type *type, Value *value) noexcept { | ||
return _create_and_append_instruction<CastInst>(type, CastOp::STATIC_CAST, value); | ||
} | ||
|
||
CastInst *Builder::bitwise_cast_(const Type *type, Value *value) noexcept { | ||
return _create_and_append_instruction<CastInst>(type, CastOp::BITWISE_CAST, value); | ||
} | ||
|
||
PhiInst *Builder::phi(const Type *type) noexcept { | ||
return _create_and_append_instruction<PhiInst>(type); | ||
} | ||
|
||
PrintInst *Builder::print(luisa::string format, luisa::span<Value *const> values) noexcept { | ||
return _create_and_append_instruction<PrintInst>(std::move(format), values); | ||
} | ||
|
||
GEPInst *Builder::gep(const Type *type, Value *base, luisa::span<Value *const> indices) noexcept { | ||
return _create_and_append_instruction<GEPInst>(type, base, indices); | ||
} | ||
|
||
LoadInst *Builder::load(const Type *type, Value *variable) noexcept { | ||
return _create_and_append_instruction<LoadInst>(type, variable); | ||
} | ||
|
||
StoreInst *Builder::store(Value *variable, Value *value) noexcept { | ||
return _create_and_append_instruction<StoreInst>(variable, value); | ||
} | ||
|
||
CommentInst *Builder::comment(luisa::string text) noexcept { | ||
return _create_and_append_instruction<CommentInst>(std::move(text)); | ||
} | ||
|
||
void Builder::set_insertion_point(Instruction *insertion_point) noexcept { | ||
_insertion_point = insertion_point; | ||
} | ||
|
||
void Builder::set_insertion_point(BasicBlock *block) noexcept { | ||
auto instruction = block ? block->instructions().tail_sentinel()->prev() : nullptr; | ||
set_insertion_point(instruction); | ||
} | ||
|
||
}// namespace luisa::compute::xir |
This file contains 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 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 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 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.