Skip to content

Commit 545f0dd

Browse files
committed
#3330: merge 2 CreateSemaphore functions into 1
1 parent 8fdeafc commit 545f0dd

File tree

5 files changed

+39
-47
lines changed

5 files changed

+39
-47
lines changed
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
CreateSemaphore
22
================
33

4-
.. doxygenfunction:: CreateSemaphore(Program &program, const CoreRange &core_range, uint32_t initial_value)
5-
6-
.. doxygenfunction:: CreateSemaphore(Program &program, const CoreRangeSet &core_range_set, uint32_t initial_value)
4+
.. doxygenfunction:: CreateSemaphore(Program &program, const std::variant<CoreRange,CoreRangeSet> &core_spec, uint32_t initial_value)

tt_metal/host_api.hpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,26 +129,13 @@ CircularBufferConfig &GetCircularBufferConfig(Program &program, CircularBufferID
129129
*
130130
* Return value: Semaphore address (uint32_t)
131131
*
132-
* | Argument | Description | Type | Valid Range | Required |
133-
* |---------------|------------------------------------------------------|-------------------------------------------------------|----------------------------------------------------------|----------|
134-
* | program | The program to which semaphore will be added to | Program & | | Yes |
135-
* | core_range | Range of the Tensix co-ordinates using the semaphore | const CoreRange & (std::pair<CoreCoord, CoreCoord>) | Pair of logical coords where first coord <= second coord | Yes |
136-
* | initial_value | Initial value of the semaphore | uint32_t | | Yes |
132+
* | Argument | Description | Type | Valid Range | Required |
133+
* |---------------|------------------------------------------------------|-----------------------------------------------------------|--------------|----------|
134+
* | program | The program to which semaphore will be added to | Program & | | Yes |
135+
* | core_spec | Range of the Tensix co-ordinates using the semaphore | const std::variant<CoreRange,CoreRangeSet> & | | Yes |
136+
* | initial_value | Initial value of the semaphore | uint32_t | | Yes |
137137
*/
138-
uint32_t CreateSemaphore(Program &program, const CoreRange &core_range, uint32_t initial_value);
139-
140-
/**
141-
* Initializes semaphore on all cores within core range (inclusive). Each core can have up to four 32B semaphores.
142-
*
143-
* Return value: Semaphore address (uint32_t)
144-
*
145-
* | Argument | Description | Type | Valid Range | Required |
146-
* |----------------|-------------------------------------------------------------|------------------------|-----------------------------------------------------------|----------|
147-
* | program | The program to which semaphore will be added to | Program & | | Yes |
148-
* | core_range_set | Set of Range of the Tensix co-ordinates using the semaphore | const CoreRangeSet & | Pairs of logical coords where first coord <= second coord | Yes |
149-
* | initial_value | Initial value of the semaphore | uint32_t | | Yes |
150-
*/
151-
uint32_t CreateSemaphore(Program &program, const CoreRangeSet &core_range_set, uint32_t initial_value);
138+
uint32_t CreateSemaphore(Program &program, const std::variant<CoreRange,CoreRangeSet> &core_spec, uint32_t initial_value);
152139

153140
/**
154141
* Allocates a DRAM or L1 buffer on device

tt_metal/impl/dispatch/command_queue.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,8 @@ void send_dispatch_kernel_to_device(Device* device) {
606606
.compile_args = dispatch_compile_args,
607607
.defines = consumer_defines});
608608

609-
tt::tt_metal::CreateSemaphore(dispatch_program, {producer_logical_core, producer_logical_core}, 2);
610-
tt::tt_metal::CreateSemaphore(dispatch_program, {consumer_logical_core, consumer_logical_core}, 0);
609+
tt::tt_metal::CreateSemaphore(dispatch_program, producer_logical_core, 2);
610+
tt::tt_metal::CreateSemaphore(dispatch_program, consumer_logical_core, 0);
611611

612612
detail::CompileProgram(device, dispatch_program);
613613
tt::tt_metal::detail::ConfigureDeviceWithProgram(device, dispatch_program);

tt_metal/impl/program.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ class Program {
151151
friend void detail::AddKernel(Program &program, Kernel *kernel);
152152
friend Kernel *detail::GetKernel(const Program &program, KernelID kernel_id);
153153

154-
friend uint32_t CreateSemaphore(Program &program, const CoreRangeSet &core_range_set, uint32_t initial_value);
155-
154+
friend uint32_t CreateSemaphore(Program &program, const std::variant<CoreRange,CoreRangeSet> &core_spec, uint32_t initial_value);
156155
void add_kernel(Kernel *kernel);
157156
Kernel *get_kernel(KernelID kernel_id) const;
158157

tt_metal/tt_metal.cpp

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -219,29 +219,37 @@ CircularBufferConfig &GetCircularBufferConfig(Program &program, CircularBufferID
219219
return detail::GetCircularBuffer(program, circular_buffer_id)->config();
220220
}
221221

222-
uint32_t CreateSemaphore(Program &program, const CoreRange &core_range, uint32_t initial_value) {
223-
return CreateSemaphore(program, CoreRangeSet({core_range}), initial_value);
224-
}
225-
226-
uint32_t CreateSemaphore(Program &program, const CoreRangeSet &core_range_set, uint32_t initial_value) {
227-
std::optional<uint32_t> address;
228-
TT_ASSERT(core_range_set.ranges().size() > 0, "Expecting a non-empty CoreRangeSet!");
229-
for (auto core_range : core_range_set.ranges()) {
230-
auto start_core = core_range.start;
231-
auto end_core = core_range.end;
232-
TT_ASSERT(start_core == end_core or start_core < end_core && "Invalid core range!");
233-
auto addr = get_semaphore_address(program, core_range);
234-
if (!address.has_value()) {
235-
address = addr;
236-
} else {
237-
TT_ASSERT(addr == address);
238-
}
239-
}
240-
TT_ASSERT(address.has_value(), "Expecting a valid Semaphore address!");
222+
uint32_t CreateSemaphore(Program &program, const std::variant<CoreRange,CoreRangeSet> &core_spec, uint32_t initial_value) {
223+
return std::visit(
224+
[&](auto&& c) -> uint32_t
225+
{
226+
using T = std::decay_t<decltype(c)>;
227+
CoreRangeSet crs({});
228+
if constexpr (std::is_same_v<T, CoreRange>) {
229+
crs = CoreRangeSet({c});
230+
} else{
231+
crs = c;
232+
}
233+
std::optional<uint32_t> address;
234+
TT_ASSERT(crs.ranges().size() > 0, "Expecting a non-empty CoreRangeSet!");
235+
for (const auto& core_range : crs.ranges()) {
236+
CoreCoord start_core = core_range.start;
237+
CoreCoord end_core = core_range.end;
238+
TT_ASSERT(start_core == end_core or start_core < end_core && "Invalid core range!");
239+
auto addr = get_semaphore_address(program, core_range);
240+
if (!address.has_value()) {
241+
address = addr;
242+
} else {
243+
TT_ASSERT(addr == address);
244+
}
245+
}
246+
TT_ASSERT(address.has_value(), "Expecting a valid Semaphore address!");
241247

242-
program.add_semaphore(core_range_set, address.value(), initial_value);
248+
program.add_semaphore(crs, address.value(), initial_value);
243249

244-
return address.value();
250+
return address.value();
251+
},
252+
core_spec);
245253
}
246254

247255
void WriteToDevice(const Buffer &buffer, const std::vector<uint32_t> &host_buffer) {

0 commit comments

Comments
 (0)