|
| 1 | +//==------ LinkedAllocaDependencies.cpp --- Scheduler unit tests -----------==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "SchedulerTest.hpp" |
| 10 | +#include "SchedulerTestUtils.hpp" |
| 11 | + |
| 12 | +using namespace cl::sycl; |
| 13 | + |
| 14 | +class MemObjMock : public cl::sycl::detail::SYCLMemObjI { |
| 15 | +public: |
| 16 | + using ContextImplPtr = std::shared_ptr<cl::sycl::detail::context_impl>; |
| 17 | + |
| 18 | + MemObjMock(const std::shared_ptr<cl::sycl::detail::MemObjRecord> &Record) |
| 19 | + : SYCLMemObjI() { |
| 20 | + MRecord = Record; |
| 21 | + } |
| 22 | + |
| 23 | + ~MemObjMock() = default; |
| 24 | + |
| 25 | + MemObjType getType() const override { return MemObjType::BUFFER; } |
| 26 | + |
| 27 | + void *allocateMem(ContextImplPtr, bool, void *, |
| 28 | + cl::sycl::detail::pi::PiEvent &) { |
| 29 | + return nullptr; |
| 30 | + } |
| 31 | + |
| 32 | + void *allocateHostMem() { return nullptr; } |
| 33 | + void releaseMem(ContextImplPtr, void *) {} |
| 34 | + void releaseHostMem(void *) {} |
| 35 | + size_t getSize() const override { return 10; } |
| 36 | +}; |
| 37 | + |
| 38 | +TEST_F(SchedulerTest, LinkedAllocaDependencies) { |
| 39 | + default_selector Selector{}; |
| 40 | + if (Selector.select_device().is_host()) { |
| 41 | + std::cerr << "Not run due to host-only environment\n"; |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + // 1. create two commands: alloca + alloca and link them |
| 46 | + // 2. call Scheduler::GraphBuilder::getOrCreateAllocaForReq |
| 47 | + detail::Requirement Req = getMockRequirement(); |
| 48 | + |
| 49 | + cl::sycl::queue Queue1; |
| 50 | + cl::sycl::detail::QueueImplPtr Q1 = cl::sycl::detail::getSyclObjImpl(Queue1); |
| 51 | + |
| 52 | + sycl::device HostDevice; |
| 53 | + std::shared_ptr<detail::queue_impl> DefaultHostQueue(new detail::queue_impl( |
| 54 | + detail::getSyclObjImpl(HostDevice), /*AsyncHandler=*/{}, |
| 55 | + /*PropList=*/{})); |
| 56 | + |
| 57 | + std::shared_ptr<cl::sycl::detail::MemObjRecord> Record{ |
| 58 | + new cl::sycl::detail::MemObjRecord(DefaultHostQueue->getContextImplPtr(), |
| 59 | + 10)}; |
| 60 | + |
| 61 | + MemObjMock MemObj(Record); |
| 62 | + Req.MSYCLMemObj = &MemObj; |
| 63 | + |
| 64 | + cl::sycl::detail::AllocaCommand AllocaCmd1(DefaultHostQueue, Req, false); |
| 65 | + Record->MAllocaCommands.push_back(&AllocaCmd1); |
| 66 | + |
| 67 | + MockCommand DepCmd(DefaultHostQueue, Req); |
| 68 | + MockCommand DepDepCmd(DefaultHostQueue, Req); |
| 69 | + DepCmd.MDeps.push_back({&DepDepCmd, DepDepCmd.getRequirement(), &AllocaCmd1}); |
| 70 | + DepDepCmd.MUsers.insert(&DepCmd); |
| 71 | + Record->MWriteLeaves.push_back(&DepCmd); |
| 72 | + |
| 73 | + MockScheduler MS; |
| 74 | + cl::sycl::detail::Command *AllocaCmd2 = |
| 75 | + MS.getOrCreateAllocaForReq(Record.get(), &Req, Q1); |
| 76 | + |
| 77 | + ASSERT_TRUE(!!AllocaCmd1.MLinkedAllocaCmd) |
| 78 | + << "No link appeared in existing command"; |
| 79 | + ASSERT_EQ(AllocaCmd1.MLinkedAllocaCmd, AllocaCmd2) << "Invalid link appeared"; |
| 80 | + ASSERT_GT(AllocaCmd1.MUsers.count(AllocaCmd2), 0u) |
| 81 | + << "New alloca isn't in users of the old one"; |
| 82 | + ASSERT_GT(AllocaCmd2->MDeps.size(), 1u) |
| 83 | + << "No deps appeared in the new alloca"; |
| 84 | + ASSERT_GT(DepCmd.MUsers.count(AllocaCmd2), 0u) |
| 85 | + << "No deps appeared for leaves of record (i.e. deps of existing alloca)"; |
| 86 | + ASSERT_TRUE(std::find_if(AllocaCmd2->MDeps.begin(), AllocaCmd2->MDeps.end(), |
| 87 | + [&](const cl::sycl::detail::DepDesc &Dep) -> bool { |
| 88 | + return Dep.MDepCommand == &AllocaCmd1; |
| 89 | + }) != AllocaCmd2->MDeps.end()) |
| 90 | + << "No deps for existing alloca appeared in new alloca"; |
| 91 | + ASSERT_TRUE(std::find_if(AllocaCmd2->MDeps.begin(), AllocaCmd2->MDeps.end(), |
| 92 | + [&](const cl::sycl::detail::DepDesc &Dep) -> bool { |
| 93 | + return Dep.MDepCommand == &DepCmd; |
| 94 | + }) != AllocaCmd2->MDeps.end()) |
| 95 | + << "No deps for leaves (deps of existing alloca) appeared in new alloca"; |
| 96 | +} |
0 commit comments