Skip to content

Commit

Permalink
[metal] Move platform/metal to backends/metal (taichi-dev#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-ye authored and archibate committed Mar 31, 2020
1 parent 7b2d372 commit 9b79aab
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 67 deletions.
4 changes: 4 additions & 0 deletions cmake/TaichiCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ file(GLOB TAICHI_CORE_SOURCE
file(GLOB TAICHI_BACKEND_SOURCE "taichi/backends/**/*.cpp" "taichi/backends/**/*.h")

file(GLOB TAICHI_CUDA_SOURCE "taichi/backends/cuda/*.cpp" "taichi/backends/cuda/*.h")
file(GLOB TAICHI_METAL_SOURCE "taichi/backends/metal/*.h" "taichi/backends/metal/*.cpp" "taichi/backends/metal/shaders/*")

list(REMOVE_ITEM TAICHI_CORE_SOURCE ${TAICHI_BACKEND_SOURCE})

if (TI_WITH_CUDA)
list(APPEND TAICHI_CORE_SOURCE ${TAICHI_CUDA_SOURCE})
endif()

# TODO(#529) include Metal source only on Apple MacOS
list(APPEND TAICHI_CORE_SOURCE ${TAICHI_METAL_SOURCE})

option(BUILD_CPP_EXAMPLES "Build legacy C++ examples" OFF)

if (BUILD_CPP_EXAMPLES)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "metal_api.h"
#include "taichi/backends/metal/api.h"

TLANG_NAMESPACE_BEGIN

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "metal_data_types.h"
#include "taichi/backends/metal/data_types.h"

TLANG_NAMESPACE_BEGIN

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include <taichi/lang_util.h>
#include <string>

#include "taichi/lang_util.h"

TLANG_NAMESPACE_BEGIN

enum class MetalDataType : int {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "taichi/platform/metal/metal_kernel_util.h"
#include "taichi/backends/metal/kernel_util.h"

#define TI_RUNTIME_HOST
#include "taichi/runtime/llvm/context.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <vector>

#include "taichi/ir/statements.h"
#include "taichi/platform/metal/metal_data_types.h"
#include "taichi/backends/metal/data_types.h"
#include "taichi/program/kernel.h"

// Data structures defined in this file may overlap with some of the Taichi data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "taichi/platform/metal/metal_runtime.h"
#include "taichi/backends/metal/runtime.h"

#include <algorithm>
#include <cstring>
Expand All @@ -12,7 +12,7 @@
#include <sys/mman.h>
#include <unistd.h>

#include "taichi/platform/metal/metal_api.h"
#include "taichi/backends/metal/api.h"
#endif // TI_PLATFORM_OSX

TLANG_NAMESPACE_BEGIN
Expand All @@ -22,7 +22,7 @@ namespace metal {

namespace {
namespace shaders {
#include "taichi/platform/metal/shaders/runtime_utils.metal.h"
#include "taichi/backends/metal/shaders/runtime_utils.metal.h"
}

using KernelTaskType = OffloadedStmt::TaskType;
Expand Down Expand Up @@ -189,7 +189,7 @@ class RuntimeListOpsMtlKernel : public CompiledMtlKernelBase {
// args[1] = child_snode_id
// Note that this args buffer has nothing to do with the one passed to Taichi
// kernel.
// See taichi/platform/metal/shaders/runtime_kernels.metal.h
// See taichi/backends/metal/shaders/runtime_kernels.metal.h
std::unique_ptr<BufferMemoryView> args_mem_;
nsobj_unique_ptr<MTLBuffer> args_buffer_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <unordered_map>

#include "taichi/lang_util.h"
#include "taichi/platform/metal/metal_kernel_util.h"
#include "taichi/backends/metal/kernel_util.h"
#include "taichi/program/profiler.h"
#include "taichi/struct/struct_metal.h"
#include "taichi/system/memory_pool.h"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "taichi/platform/metal/shaders/prolog.h"
#include "taichi/backends/metal/shaders/prolog.h"

#ifdef TI_INSIDE_METAL_CODEGEN

Expand All @@ -19,14 +19,18 @@ static_assert(false, "Do not include");

#endif // TI_INSIDE_METAL_CODEGEN

// clang-format off
METAL_BEGIN_HELPERS_DEF
STR(template <typename T, typename G> T union_cast(G g) {
// For some reason, if I emit taichi/common.h's union_cast(), Metal failed
// to compile. More strangely, if I copy the generated code to XCode as a
// Metal kernel, it compiled successfully...
static_assert(sizeof(T) == sizeof(G), "Size mismatch");
return *reinterpret_cast<thread const T *>(&g);
}
STR(
// clang-format on
template <typename T, typename G>
T union_cast(G g) {
// For some reason, if I emit taichi/common.h's union_cast(), Metal failed
// to compile. More strangely, if I copy the generated code to XCode as a
// Metal kernel, it compiled successfully...
static_assert(sizeof(T) == sizeof(G), "Size mismatch");
return *reinterpret_cast<thread const T *>(&g);
}

inline int ifloordiv(int lhs, int rhs) {
const int intm = (lhs / rhs);
Expand Down Expand Up @@ -87,10 +91,13 @@ STR(template <typename T, typename G> T union_cast(G g) {
metal::memory_order_relaxed);
}
return old_val;
})
}
// clang-format off
)
METAL_END_HELPERS_DEF
// clang-format on

#undef METAL_BEGIN_HELPERS_DEF
#undef METAL_END_HELPERS_DEF

#include "taichi/platform/metal/shaders/epilog.h"
#include "taichi/backends/metal/shaders/epilog.h"
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

#define byte char

#include "taichi/platform/metal/shaders/atomic_stubs.h"
#include "taichi/backends/metal/shaders/atomic_stubs.h"

#endif // TI_INSIDE_METAL_CODEGEN
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#ifndef TI_METAL_NESTED_INCLUDE

#define TI_METAL_NESTED_INCLUDE
#include "taichi/platform/metal/shaders/runtime_utils.metal.h"
#include "taichi/backends/metal/shaders/runtime_utils.metal.h"
#undef TI_METAL_NESTED_INCLUDE

#else
#include "taichi/platform/metal/shaders/runtime_utils.metal.h"
#include "taichi/backends/metal/shaders/runtime_utils.metal.h"
#endif // TI_METAL_NESTED_INCLUDE

#include "taichi/platform/metal/shaders/prolog.h"
#include "taichi/backends/metal/shaders/prolog.h"

#ifdef TI_INSIDE_METAL_CODEGEN

Expand Down Expand Up @@ -39,23 +39,26 @@ struct Runtime {

#endif // TI_INSIDE_METAL_CODEGEN

// clang-format off
METAL_BEGIN_RUNTIME_KERNELS_DEF
STR(kernel void clear_list(device byte *runtime_addr[[buffer(0)]],
device int *args[[buffer(1)]],
const uint utid_[[thread_position_in_grid]]) {
if (utid_ > 0)
return;
int child_snode_id = args[1];
device ListManager *child_list =
&(reinterpret_cast<device Runtime *>(runtime_addr)
->snode_lists[child_snode_id]);
clear(child_list);
}
STR(
// clang-format on
kernel void clear_list(device byte *runtime_addr [[buffer(0)]],
device int *args [[buffer(1)]],
const uint utid_ [[thread_position_in_grid]]) {
if (utid_ > 0)
return;
int child_snode_id = args[1];
device ListManager *child_list =
&(reinterpret_cast<device Runtime *>(runtime_addr)
->snode_lists[child_snode_id]);
clear(child_list);
}

kernel void element_listgen(device byte *runtime_addr[[buffer(0)]],
device byte *root_addr[[buffer(1)]],
device int *args[[buffer(2)]],
const uint utid_[[thread_position_in_grid]]) {
kernel void element_listgen(device byte *runtime_addr [[buffer(0)]],
device byte *root_addr [[buffer(1)]],
device int *args [[buffer(2)]],
const uint utid_ [[thread_position_in_grid]]) {
device Runtime *runtime =
reinterpret_cast<device Runtime *>(runtime_addr);
device byte *list_data_addr =
Expand Down Expand Up @@ -86,10 +89,13 @@ STR(kernel void clear_list(device byte *runtime_addr[[buffer(0)]],
append(child_list, child_elem, list_data_addr);
}
}
})
}
// clang-format off
)
METAL_END_RUNTIME_KERNELS_DEF
// clang-format on

#undef METAL_BEGIN_RUNTIME_KERNELS_DEF
#undef METAL_END_RUNTIME_KERNELS_DEF

#include "taichi/platform/metal/shaders/epilog.h"
#include "taichi/backends/metal/shaders/epilog.h"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "taichi/platform/metal/shaders/prolog.h"
#include "taichi/backends/metal/shaders/prolog.h"

#ifdef TI_INSIDE_METAL_CODEGEN

Expand All @@ -25,8 +25,11 @@ static_assert(taichi_max_num_indices == 8,

#endif // TI_INSIDE_METAL_CODEGEN

// clang-format off
METAL_BEGIN_RUNTIME_STRUCTS_DEF
STR(constant constexpr int kTaichiMaxNumIndices = 8;
STR(
// clang-format on
constant constexpr int kTaichiMaxNumIndices = 8;

struct ListgenElement {
int32_t coords[kTaichiMaxNumIndices];
Expand Down Expand Up @@ -61,10 +64,13 @@ STR(constant constexpr int kTaichiMaxNumIndices = 8;
};

Extractor extractors[kTaichiMaxNumIndices];
};)
};
// clang-format off
)
METAL_END_RUNTIME_STRUCTS_DEF
// clang-format on

#undef METAL_BEGIN_RUNTIME_STRUCTS_DEF
#undef METAL_END_RUNTIME_STRUCTS_DEF

#include "taichi/platform/metal/shaders/epilog.h"
#include "taichi/backends/metal/shaders/epilog.h"
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#ifndef TI_METAL_NESTED_INCLUDE

#define TI_METAL_NESTED_INCLUDE
#include "taichi/platform/metal/shaders/runtime_structs.metal.h"
#include "taichi/backends/metal/shaders/runtime_structs.metal.h"
#undef TI_METAL_NESTED_INCLUDE

#else
#include "taichi/platform/metal/shaders/runtime_structs.metal.h"
#include "taichi/backends/metal/shaders/runtime_structs.metal.h"
#endif // TI_METAL_NESTED_INCLUDE

#include "taichi/platform/metal/shaders/prolog.h"
#include "taichi/backends/metal/shaders/prolog.h"

#ifdef TI_INSIDE_METAL_CODEGEN

Expand Down Expand Up @@ -112,11 +112,12 @@ STR(
const int addition = (((l >> ex.acc_offset) & mask) << ex.start);
child_elem->coords[i] = (parent_elem.coords[i] | addition);
}
})
}
)
METAL_END_RUNTIME_UTILS_DEF
// clang-format on

#undef METAL_BEGIN_RUNTIME_UTILS_DEF
#undef METAL_END_RUNTIME_UTILS_DEF

#include "taichi/platform/metal/shaders/epilog.h"
#include "taichi/backends/metal/shaders/epilog.h"
4 changes: 2 additions & 2 deletions taichi/codegen/codegen_metal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace metal {
namespace {

namespace shaders {
#include "taichi/platform/metal/shaders/runtime_structs.metal.h"
#include "taichi/backends/metal/shaders/runtime_structs.metal.h"
} // namespace shaders

using BuffersEnum = MetalKernelAttributes::Buffers;
Expand Down Expand Up @@ -488,7 +488,7 @@ class MetalKernelCodegen : public IRVisitor {
emit("using byte = uchar;");
emit("");
#define TI_INSIDE_METAL_CODEGEN
#include "taichi/platform/metal/shaders/helpers.metal.h"
#include "taichi/backends/metal/shaders/helpers.metal.h"
line_appender_.append_raw(kMetalHelpersSourceCode);
#undef TI_INSIDE_METAL_CODEGEN
emit("");
Expand Down
6 changes: 3 additions & 3 deletions taichi/codegen/codegen_metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

#include "taichi/inc/constants.h"
#include "taichi/lang_util.h"
#include "taichi/platform/metal/metal_data_types.h"
#include "taichi/platform/metal/metal_kernel_util.h"
#include "taichi/platform/metal/metal_runtime.h"
#include "taichi/backends/metal/data_types.h"
#include "taichi/backends/metal/kernel_util.h"
#include "taichi/backends/metal/runtime.h"
#include "taichi/program/program.h"
#include "taichi/struct/struct_metal.h"

Expand Down
7 changes: 3 additions & 4 deletions taichi/program/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

#include "program.h"

#include <taichi/common/task.h>
#include <taichi/platform/metal/metal_api.h>
#include <taichi/platform/opengl/opengl_api.h>

#include "taichi/common/task.h"
#include "taichi/backends/metal/api.h"
#include "taichi/platform/opengl/opengl_api.h"
#include "taichi/codegen/codegen_cuda.h"
#include "taichi/codegen/codegen_metal.h"
#include "taichi/codegen/codegen_opengl.h"
Expand Down
2 changes: 1 addition & 1 deletion taichi/program/program.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "taichi/ir/snode.h"
#include "taichi/lang_util.h"
#include "taichi/llvm/llvm_context.h"
#include "taichi/platform/metal/metal_runtime.h"
#include "taichi/backends/metal/runtime.h"
#include "taichi/platform/opengl/opengl_kernel_util.h"
#include "taichi/program/kernel.h"
#include "taichi/program/profiler.h"
Expand Down
2 changes: 1 addition & 1 deletion taichi/python/export_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <taichi/system/profiler.h>
#include <taichi/system/memory_usage_monitor.h>
#include <taichi/system/unit_dll.h>
#include <taichi/platform/metal/metal_api.h>
#include <taichi/backends/metal/api.h>
#include <taichi/platform/opengl/opengl_api.h>
#if defined(TI_WITH_CUDA)
#include <cuda_runtime_api.h>
Expand Down
12 changes: 6 additions & 6 deletions taichi/struct/struct_metal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
#include <vector>

#include "taichi/math/arithmetic.h"
#include "taichi/platform/metal/metal_data_types.h"
#include "taichi/platform/metal/metal_kernel_util.h"
#include "taichi/backends/metal/data_types.h"
#include "taichi/backends/metal/kernel_util.h"
#include "taichi/util/line_appender.h"

TLANG_NAMESPACE_BEGIN
namespace metal {
namespace {
namespace shaders {
#define TI_INSIDE_METAL_CODEGEN
#include "taichi/platform/metal/shaders/runtime_kernels.metal.h"
#include "taichi/platform/metal/shaders/runtime_structs.metal.h"
#include "taichi/platform/metal/shaders/runtime_utils.metal.h"
#include "taichi/backends/metal/shaders/runtime_kernels.metal.h"
#include "taichi/backends/metal/shaders/runtime_structs.metal.h"
#include "taichi/backends/metal/shaders/runtime_utils.metal.h"
#undef TI_INSIDE_METAL_CODEGEN

#include "taichi/platform/metal/shaders/runtime_structs.metal.h"
#include "taichi/backends/metal/shaders/runtime_structs.metal.h"

} // namespace shaders

Expand Down

0 comments on commit 9b79aab

Please sign in to comment.