简体   繁体   English

使用 genrule 获取 ISPC 的 output

[英]Fetch output of ISPC using a genrule

I am trying to make use of ISPC ( Implicit SPMD Program Compiler ) using Bazel .我正在尝试使用Bazel来使用 ISPC(隐式 SPMD 程序编译器)。 Therefore, I started to implement rules_ispc .因此,我开始实施rules_ispc

Unfortunately, I run into an issue with generating files using ispc .不幸的是,我遇到了使用ispc生成文件的问题。

You can reproduce my issue by:您可以通过以下方式重现我的问题:

git clone https://github.com/Vertexwahn/rules_ispc.git
cd rules_ispc
cd tests
bazel build //example:main

The following error will be generated:将生成以下错误:

ERROR: /home/vertexwahn/rules_ispc/tests/example/BUILD.bazel:3:16: declared output 'example/square.o' was not created by genrule.错误:/home/vertexwahn/rules_ispc/tests/example/BUILD.bazel:3:16: declared output 'example/square.o' 不是由 genrule 创建的。 This is probably because the genrule actually didn't create this output, or because the output was a directory and the genrule was run remotely (note that only the contents of declared file outputs are copied from genrules run remotely) ERROR: /home/vertexwahn/rules_ispc/tests/example/BUILD.bazel:3:16: declared output 'example/square.h' was not created by genrule.这可能是因为 genrule 实际上没有创建这个 output,或者因为 output 是一个目录并且 genrule 是远程运行的(请注意,只有声明的文件输出的内容是从远程运行的 genrules 复制的)错误:/home/vertexwahn /rules_ispc/tests/example/BUILD.bazel:3:16: 声明 output 'example/square.h' 不是由 genrule 创建的。 This is probably because the genrule actually didn't create this output, or because the output was a directory and the genrule was run remotely (note that only the contents of declared file outputs are copied from genrules run remotely) ERROR: /home/vertexwahn/rules_ispc/tests/example/BUILD.bazel:3:16: Executing genrule //example:square_ispc_gen failed: not all outputs were created or valid这可能是因为 genrule 实际上没有创建这个 output,或者因为 output 是一个目录并且 genrule 是远程运行的(请注意,只有声明的文件输出的内容是从远程运行的 genrules 复制的)错误:/home/vertexwahn /rules_ispc/tests/example/BUILD.bazel:3:16: 执行 genrule //example:square_ispc_gen 失败:并非所有输出都已创建或有效

I see the same error on Ubuntu 20.04 , Ubuntu 22.04 , Windows Server 2019 , Windows Server 2022 , macOS 11 and macOS 12 .我在Ubuntu 20.04、Ubuntu 22.04、Windows Server 2019、Windows Server 2022macOS 11macOS 12上看到了同样的错误。 For details see my CI workflow .有关详细信息,请参阅我的CI 工作流程

My BUILD.bazel file looks like this:我的BUILD.bazel文件如下所示:

load("@rules_ispc//:ispc.bzl", "ispc_cc_library")

ispc_cc_library(
    name = "square",
    srcs = ["square.ispc"],
)

cc_binary(
    name = "main",
    srcs = ["main.cpp"],
    deps = [":square"],
)

ispc_cc_library calls ISPC internal to generate a header file and an o file: ispc_cc_library内部调用ISPC生成一个header文件和一个o文件:

def ispc_cc_library(name, srcs,  target_compatible_with = [], **kwargs):
    for ispc_source_file in srcs:
        generted_header_filename = name + ".h"
        native.genrule(
            name = "%s_ispc_gen" % name,
            srcs = [ispc_source_file],
            outs = [name + ".o", generted_header_filename],
            cmd = select({
                "@platforms//os:linux": "$(location @ispc_linux_x86_64//:ispc) --target=avx2 --arch=x86-64 $(locations %s) --header-outfile=%s -o %s.o" % (ispc_source_file, generted_header_filename, name),
                "@platforms//os:osx": "$(location @ispc_osx_x86_64//:ispc) --target=avx2 --arch=x86-64 $(locations %s) --header-outfile=%s -o %s.o" % (ispc_source_file, generted_header_filename, name),
                "@platforms//os:windows": "$(location @ispc_windows_x86_64//:ispc) --target=avx2 --target-os=windows --arch=x86-64 $(locations %s) --header-outfile=%s -o %s.o" % (ispc_source_file, generted_header_filename, name),
            }),
            tools = select({
                "@platforms//os:linux": ["@ispc_linux_x86_64//:ispc"],
                "@platforms//os:osx": ["@ispc_osx_x86_64//:ispc"],
                "@platforms//os:windows": ["@ispc_windows_x86_64//:ispc"],
            }),
            target_compatible_with = target_compatible_with,
        )
        native.cc_library(
            name = name,
            srcs = [name + ".o"],
            hdrs = [name + ".h"],
            target_compatible_with = target_compatible_with,
            **kwargs
        )

I wonder how to fetch the generated files of ISPC correctly with the genrule .我想知道如何使用genrule正确获取 ISPC 生成的文件。 Any hints welcome!欢迎任何提示!

More details:更多细节:

square.ispc :广场.ispc

export void square(uniform float vin[], uniform float vout[],
                   uniform int count) {
    foreach (index = 0 ... count) {
        float v = vin[index];
        v = v * v;
        vout[index] = v;
    }
}

The header generated by ISPC should look this: ISPC 生成的 header 应该是这样的:

square.h正方形.h

//
// C:/rules_ispc/tests/example/square.h
// (Header automatically generated by the ispc compiler.)
// DO NOT EDIT THIS FILE.
//

#pragma once
#include <stdint.h>



#ifdef __cplusplus
namespace ispc { /* namespace */
#endif // __cplusplus

#ifndef __ISPC_ALIGN__
#if defined(__clang__) || !defined(_MSC_VER)
// Clang, GCC, ICC
#define __ISPC_ALIGN__(s) __attribute__((aligned(s)))
#define __ISPC_ALIGNED_STRUCT__(s) struct __ISPC_ALIGN__(s)
#else
// Visual Studio
#define __ISPC_ALIGN__(s) __declspec(align(s))
#define __ISPC_ALIGNED_STRUCT__(s) __ISPC_ALIGN__(s) struct
#endif
#endif


///////////////////////////////////////////////////////////////////////////
// Functions exported from ispc code
///////////////////////////////////////////////////////////////////////////
#if defined(__cplusplus) && (! defined(__ISPC_NO_EXTERN_C) || !__ISPC_NO_EXTERN_C )
extern "C" {
#endif // __cplusplus
    extern void square(float * vin, float * vout, int32_t count);
#if defined(__cplusplus) && (! defined(__ISPC_NO_EXTERN_C) || !__ISPC_NO_EXTERN_C )
} /* end extern C */
#endif // __cplusplus


#ifdef __cplusplus
} /* namespace */
#endif // __cplusplus

If I search for the generated header file square.h in the directories generated by Bazel it seems that this file does not exist, ie is not generated.如果我在 Bazel 生成的目录中搜索生成的 header 文件square.h ,似乎这个文件不存在,即没有生成。

You need to perform $(location) expansion on the outputs, too, since Bazel will assign them paths in the bazel-out/ output tree.您还需要对输出执行$(location)扩展,因为 Bazel 会在bazel-out/ output 树中为它们分配路径。

diff --git a/ispc.bzl b/ispc.bzl
index 1fca9b4..a1b9450 100644
--- a/ispc.bzl
+++ b/ispc.bzl
@@ -6,9 +6,9 @@ def ispc_cc_library(name, srcs,  target_compatible_with = [], **kwargs):
             srcs = [ispc_source_file],
             outs = [name + ".o", generted_header_filename],
             cmd = select({
-                "@platforms//os:linux": "$(location @ispc_linux_x86_64//:ispc) --target=avx2 --arch=x86-64 $(locations %s) --header-outfile=%s -o %s.o" % (ispc_source_file, generted_header_filename, name),
-                "@platforms//os:osx": "$(location @ispc_osx_x86_64//:ispc) --target=avx2 --arch=x86-64 $(locations %s) --header-outfile=%s -o %s.o" % (ispc_source_file, generted_header_filename, name),
-                "@platforms//os:windows": "$(location @ispc_windows_x86_64//:ispc) --target=avx2 --target-os=windows --arch=x86-64 $(locations %s) --header-outfile=%s -o %s.o" % (ispc_source_file, generted_header_filename, name),
+                "@platforms//os:linux": "$(location @ispc_linux_x86_64//:ispc) --target=avx2 --arch=x86-64 $(locations %s) --header-outfile=$(location %s) -o $(location %s.o)" % (ispc_source_file, generted_header_filename, name),
+                "@platforms//os:osx": "$(location @ispc_osx_x86_64//:ispc) --target=avx2 --arch=x86-64 $(locations %s) --header-outfile=$(location %s) -o $(location %s.o)" % (ispc_source_file, generted_header_filename, name),
+                "@platforms//os:windows": "$(location @ispc_windows_x86_64//:ispc) --target=avx2 --target-os=windows --arch=x86-64 $(locations %s) --header-outfile=$(location %s) -o $(location %s.o)" % (ispc_source_file, generted_header_filename, name),
             }),
             tools = select({
                 "@platforms//os:linux": ["@ispc_linux_x86_64//:ispc"],
diff --git a/tests/example/main.cpp b/tests/example/main.cpp
index 44f7feb..1883757 100644
--- a/tests/example/main.cpp
+++ b/tests/example/main.cpp
@@ -1,4 +1,4 @@
-#include "square.h"
+#include "example/square.h"
 
 #include <stdio.h>
 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM