Skip to content

Commit

Permalink
refactor PooledObject
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-Leo-Smith committed Feb 4, 2025
1 parent 0cdd7f7 commit 9b788b9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
12 changes: 5 additions & 7 deletions include/luisa/xir/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LC_XIR_API Metadata : public IntrusiveForwardNode<Metadata> {

public:
explicit Metadata(Pool *pool) noexcept;
[[nodiscard]] Pool *pool() const noexcept override { return _pool; }
[[nodiscard]] Pool *pool() noexcept override { return _pool; }
[[nodiscard]] virtual DerivedMetadataTag derived_metadata_tag() const noexcept = 0;
[[nodiscard]] virtual Metadata *clone(Pool *pool) const noexcept = 0;
LUISA_XIR_DEFINED_ISA_METHOD(Metadata, metadata)
Expand All @@ -31,14 +31,12 @@ class LC_XIR_API DerivedMetadata : public Base {
using derived_metadata_type = Derived;
using Super = DerivedMetadata;
using Base::Base;

[[nodiscard]] static constexpr auto
static_derived_metadata_tag() noexcept {
return tag;
}
static_derived_metadata_tag() noexcept { return tag; }

[[nodiscard]] DerivedMetadataTag
derived_metadata_tag() const noexcept final {
return static_derived_metadata_tag();
}
derived_metadata_tag() const noexcept final { return static_derived_metadata_tag(); }
};

using MetadataList = IntrusiveForwardList<Metadata>;
Expand Down
9 changes: 7 additions & 2 deletions include/luisa/xir/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ class LC_XIR_API PooledObject {

public:
virtual ~PooledObject() noexcept = default;
[[nodiscard]] virtual Pool *pool() const noexcept = 0;

[[nodiscard]] virtual Pool *pool() noexcept = 0;
[[nodiscard]] const Pool *pool() const noexcept {
return const_cast<PooledObject *>(this)->pool();
}

// make the object pinned to its memory location
PooledObject(PooledObject &&) noexcept = delete;
Expand Down Expand Up @@ -74,7 +78,8 @@ class LC_XIR_API PoolOwner {
public:
explicit PoolOwner(size_t init_pool_cap = 0u) noexcept;
virtual ~PoolOwner() noexcept = default;
[[nodiscard]] Pool *pool() const noexcept { return _pool.get(); }
[[nodiscard]] Pool *pool() noexcept { return _pool.get(); }
[[nodiscard]] const Pool *pool() const noexcept { return _pool.get(); }
};

}// namespace luisa::compute::xir
2 changes: 1 addition & 1 deletion include/luisa/xir/use.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LC_XIR_API Use final : public IntrusiveForwardNode<Use> {
public:
explicit Use(User *user, Value *value = nullptr) noexcept;
void set_value(Value *value) noexcept;
[[nodiscard]] Pool *pool() const noexcept override;
[[nodiscard]] Pool *pool() noexcept override;
[[nodiscard]] auto value() noexcept { return _value; }
[[nodiscard]] auto value() const noexcept { return const_cast<const Value *>(_value); }
[[nodiscard]] auto user() noexcept { return _user; }
Expand Down
12 changes: 6 additions & 6 deletions include/luisa/xir/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class LC_XIR_API GlobalValueModuleMixin {
protected:
explicit GlobalValueModuleMixin(Module *module) noexcept;
~GlobalValueModuleMixin() noexcept = default;
[[nodiscard]] Pool *_pool_from_parent_module() const noexcept;
[[nodiscard]] Pool *_pool_from_parent_module() noexcept;

public:
[[nodiscard]] Module *parent_module() noexcept { return _parent_module; }
Expand All @@ -86,7 +86,7 @@ class DerivedGlobalValue : public DerivedValue<Derived, tag, Base>,
explicit DerivedGlobalValue(Module *module, Args &&...args) noexcept
: DerivedValue<Derived, tag, Base>{std::forward<Args>(args)...},
GlobalValueModuleMixin{module} {}
[[nodiscard]] Pool *pool() const noexcept final { return _pool_from_parent_module(); }
[[nodiscard]] Pool *pool() noexcept final { return _pool_from_parent_module(); }
};

class LC_XIR_API LocalValueFunctionMixin {
Expand All @@ -100,7 +100,7 @@ class LC_XIR_API LocalValueFunctionMixin {
explicit LocalValueFunctionMixin(Function *function) noexcept;
~LocalValueFunctionMixin() noexcept = default;
void _set_parent_function(Function *function) noexcept;
[[nodiscard]] Pool *_pool_from_parent_function() const noexcept;
[[nodiscard]] Pool *_pool_from_parent_function() noexcept;

public:
[[nodiscard]] Function *parent_function() noexcept { return _parent_function; }
Expand All @@ -119,7 +119,7 @@ class DerivedFunctionScopeValue : public DerivedValue<Derived, tag, Base>,
explicit DerivedFunctionScopeValue(Function *function, Args &&...args) noexcept
: DerivedValue<Derived, tag, Base>{std::forward<Args>(args)...},
LocalValueFunctionMixin{function} {}
[[nodiscard]] Pool *pool() const noexcept final { return _pool_from_parent_function(); }
[[nodiscard]] Pool *pool() noexcept final { return _pool_from_parent_function(); }
};

class LC_XIR_API LocalValueBlockMixin {
Expand All @@ -132,7 +132,7 @@ class LC_XIR_API LocalValueBlockMixin {
explicit LocalValueBlockMixin(BasicBlock *block) noexcept;
~LocalValueBlockMixin() noexcept = default;
void _set_parent_block(BasicBlock *block) noexcept;
[[nodiscard]] Pool *_pool_from_parent_block() const noexcept;
[[nodiscard]] Pool *_pool_from_parent_block() noexcept;

public:
[[nodiscard]] BasicBlock *parent_block() noexcept { return _parent_block; }
Expand All @@ -153,7 +153,7 @@ class DerivedBlockScopeValue : public DerivedValue<Derived, tag, Base>,
explicit DerivedBlockScopeValue(BasicBlock *block, Args &&...args) noexcept
: DerivedValue<Derived, tag, Base>{std::forward<Args>(args)...},
LocalValueBlockMixin{block} {}
[[nodiscard]] Pool *pool() const noexcept final { return _pool_from_parent_block(); }
[[nodiscard]] Pool *pool() noexcept final { return _pool_from_parent_block(); }
};

}// namespace luisa::compute::xir
4 changes: 1 addition & 3 deletions src/xir/use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ void Use::set_value(Value *value) noexcept {
_value = value;
}

Pool *Use::pool() const noexcept {
return user()->pool();
}
Pool *Use::pool() noexcept { return user()->pool(); }

}// namespace luisa::compute::xir
6 changes: 3 additions & 3 deletions src/xir/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ GlobalValueModuleMixin::GlobalValueModuleMixin(Module *module) noexcept : _paren
LUISA_DEBUG_ASSERT(_parent_module != nullptr, "Module must not be null.");
}

Pool *GlobalValueModuleMixin::_pool_from_parent_module() const noexcept {
Pool *GlobalValueModuleMixin::_pool_from_parent_module() noexcept {
return parent_module()->pool();
}

Expand All @@ -36,7 +36,7 @@ void LocalValueFunctionMixin::_set_parent_function(Function *function) noexcept
_parent_function = function;
}

Pool *LocalValueFunctionMixin::_pool_from_parent_function() const noexcept {
Pool *LocalValueFunctionMixin::_pool_from_parent_function() noexcept {
return parent_function()->pool();
}

Expand All @@ -58,7 +58,7 @@ void LocalValueBlockMixin::_set_parent_block(BasicBlock *block) noexcept {
_parent_block = block;
}

Pool *LocalValueBlockMixin::_pool_from_parent_block() const noexcept {
Pool *LocalValueBlockMixin::_pool_from_parent_block() noexcept {
return parent_block()->pool();
}

Expand Down

0 comments on commit 9b788b9

Please sign in to comment.