Move a bunch of the CLC stuff from src/microsoft to common code

The D3D12-specific stuff isn't useful to have in common code but all the
stuff to invoke clang really should be common.

v2: Rebase (Lionel)

v3: Define a new clc_libclc_new_dxil() entrypoint to create a clc
    context with DXIL nir_options (Jesse)

v4: Fixup meson build (Lionel)

Signed-off-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Reviewed-by: Jesse Natalie <jenatali@microsoft.com>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9156>
This commit is contained in:
Jason Ekstrand
2021-02-18 21:19:24 -06:00
committed by Marge Bot
parent a9c49a0541
commit 1506ea2ecb
12 changed files with 671 additions and 533 deletions
+314
View File
@@ -0,0 +1,314 @@
/*
* Copyright © Microsoft Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "nir/nir.h"
#include "nir/nir_serialize.h"
#include "glsl_types.h"
#include "nir_types.h"
#include "clc.h"
#include "clc_helpers.h"
#include "spirv/nir_spirv.h"
#include "util/u_debug.h"
#include <stdlib.h>
enum clc_debug_flags {
CLC_DEBUG_DUMP_SPIRV = 1 << 0,
CLC_DEBUG_VERBOSE = 1 << 1,
};
static const struct debug_named_value clc_debug_options[] = {
{ "dump_spirv", CLC_DEBUG_DUMP_SPIRV, "Dump spirv blobs" },
{ "verbose", CLC_DEBUG_VERBOSE, NULL },
DEBUG_NAMED_VALUE_END
};
DEBUG_GET_ONCE_FLAGS_OPTION(debug_clc, "CLC_DEBUG", clc_debug_options, 0)
static void
clc_print_kernels_info(const struct clc_parsed_spirv *obj)
{
fprintf(stdout, "Kernels:\n");
for (unsigned i = 0; i < obj->num_kernels; i++) {
const struct clc_kernel_arg *args = obj->kernels[i].args;
bool first = true;
fprintf(stdout, "\tvoid %s(", obj->kernels[i].name);
for (unsigned j = 0; j < obj->kernels[i].num_args; j++) {
if (!first)
fprintf(stdout, ", ");
else
first = false;
switch (args[j].address_qualifier) {
case CLC_KERNEL_ARG_ADDRESS_GLOBAL:
fprintf(stdout, "__global ");
break;
case CLC_KERNEL_ARG_ADDRESS_LOCAL:
fprintf(stdout, "__local ");
break;
case CLC_KERNEL_ARG_ADDRESS_CONSTANT:
fprintf(stdout, "__constant ");
break;
default:
break;
}
if (args[j].type_qualifier & CLC_KERNEL_ARG_TYPE_VOLATILE)
fprintf(stdout, "volatile ");
if (args[j].type_qualifier & CLC_KERNEL_ARG_TYPE_CONST)
fprintf(stdout, "const ");
if (args[j].type_qualifier & CLC_KERNEL_ARG_TYPE_RESTRICT)
fprintf(stdout, "restrict ");
fprintf(stdout, "%s %s", args[j].type_name, args[j].name);
}
fprintf(stdout, ");\n");
}
}
static void
clc_libclc_optimize(nir_shader *s)
{
bool progress;
do {
progress = false;
NIR_PASS(progress, s, nir_split_var_copies);
NIR_PASS(progress, s, nir_opt_copy_prop_vars);
NIR_PASS(progress, s, nir_lower_var_copies);
NIR_PASS(progress, s, nir_lower_vars_to_ssa);
NIR_PASS(progress, s, nir_copy_prop);
NIR_PASS(progress, s, nir_opt_remove_phis);
NIR_PASS(progress, s, nir_opt_dce);
NIR_PASS(progress, s, nir_opt_if, true);
NIR_PASS(progress, s, nir_opt_dead_cf);
NIR_PASS(progress, s, nir_opt_cse);
NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
NIR_PASS(progress, s, nir_opt_algebraic);
NIR_PASS(progress, s, nir_opt_constant_folding);
NIR_PASS(progress, s, nir_opt_undef);
NIR_PASS(progress, s, nir_lower_undef_to_zero);
NIR_PASS(progress, s, nir_opt_deref);
} while (progress);
}
struct clc_libclc {
const nir_shader *libclc_nir;
};
struct clc_libclc *
clc_libclc_new(const struct clc_logger *logger, const struct clc_libclc_options *options)
{
struct clc_libclc *ctx = rzalloc(NULL, struct clc_libclc);
if (!ctx) {
clc_error(logger, "D3D12: failed to allocate a clc_libclc");
return NULL;
}
const struct spirv_to_nir_options libclc_spirv_options = {
.environment = NIR_SPIRV_OPENCL,
.create_library = true,
.constant_addr_format = nir_address_format_32bit_index_offset_pack64,
.global_addr_format = nir_address_format_32bit_index_offset_pack64,
.shared_addr_format = nir_address_format_32bit_offset_as_64bit,
.temp_addr_format = nir_address_format_32bit_offset_as_64bit,
.float_controls_execution_mode = FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32,
.caps = {
.address = true,
.float64 = true,
.int8 = true,
.int16 = true,
.int64 = true,
.kernel = true,
},
};
glsl_type_singleton_init_or_ref();
nir_shader *s = nir_load_libclc_shader(64, NULL, &libclc_spirv_options, options->nir_options);
if (!s) {
clc_error(logger, "D3D12: spirv_to_nir failed on libclc blob");
ralloc_free(ctx);
return NULL;
}
if (options && options->optimize)
clc_libclc_optimize(s);
ralloc_steal(ctx, s);
ctx->libclc_nir = s;
return ctx;
}
void clc_free_libclc(struct clc_libclc *ctx)
{
ralloc_free(ctx);
glsl_type_singleton_decref();
}
const nir_shader *clc_libclc_get_clc_shader(struct clc_libclc *ctx)
{
return ctx->libclc_nir;
}
void clc_libclc_serialize(struct clc_libclc *context,
void **serialized,
size_t *serialized_size)
{
struct blob tmp;
blob_init(&tmp);
nir_serialize(&tmp, context->libclc_nir, true);
blob_finish_get_buffer(&tmp, serialized, serialized_size);
}
void clc_libclc_free_serialized(void *serialized)
{
free(serialized);
}
struct clc_libclc *
clc_libclc_deserialize(const void *serialized, size_t serialized_size)
{
struct clc_libclc *ctx = rzalloc(NULL, struct clc_libclc);
if (!ctx) {
return NULL;
}
glsl_type_singleton_init_or_ref();
struct blob_reader tmp;
blob_reader_init(&tmp, serialized, serialized_size);
nir_shader *s = nir_deserialize(NULL, NULL, &tmp);
if (!s) {
ralloc_free(ctx);
return NULL;
}
ralloc_steal(ctx, s);
ctx->libclc_nir = s;
return ctx;
}
bool
clc_compile_c_to_spir(const struct clc_compile_args *args,
const struct clc_logger *logger,
struct clc_binary *out_spir)
{
return clc_c_to_spir(args, logger, out_spir) >= 0;
}
void
clc_free_spir(struct clc_binary *spir)
{
clc_free_spir_binary(spir);
}
bool
clc_compile_spir_to_spirv(const struct clc_binary *in_spir,
const struct clc_logger *logger,
struct clc_binary *out_spirv)
{
if (clc_spir_to_spirv(in_spir, logger, out_spirv) < 0)
return false;
if (debug_get_option_debug_clc() & CLC_DEBUG_DUMP_SPIRV)
clc_dump_spirv(out_spirv, stdout);
return true;
}
void
clc_free_spirv(struct clc_binary *spirv)
{
clc_free_spirv_binary(spirv);
}
bool
clc_compile_c_to_spirv(const struct clc_compile_args *args,
const struct clc_logger *logger,
struct clc_binary *out_spirv)
{
if (clc_c_to_spirv(args, logger, out_spirv) < 0)
return false;
if (debug_get_option_debug_clc() & CLC_DEBUG_DUMP_SPIRV)
clc_dump_spirv(out_spirv, stdout);
return true;
}
bool
clc_link_spirv(const struct clc_linker_args *args,
const struct clc_logger *logger,
struct clc_binary *out_spirv)
{
if (clc_link_spirv_binaries(args, logger, out_spirv) < 0)
return false;
if (debug_get_option_debug_clc() & CLC_DEBUG_DUMP_SPIRV)
clc_dump_spirv(out_spirv, stdout);
return true;
}
bool
clc_parse_spirv(const struct clc_binary *in_spirv,
const struct clc_logger *logger,
struct clc_parsed_spirv *out_data)
{
if (!clc_spirv_get_kernels_info(in_spirv,
&out_data->kernels,
&out_data->num_kernels,
&out_data->spec_constants,
&out_data->num_spec_constants,
logger))
return false;
if (debug_get_option_debug_clc() & CLC_DEBUG_VERBOSE)
clc_print_kernels_info(out_data);
return true;
}
void clc_free_parsed_spirv(struct clc_parsed_spirv *data)
{
clc_free_kernels_info(data->kernels, data->num_kernels);
}
bool
clc_specialize_spirv(const struct clc_binary *in_spirv,
const struct clc_parsed_spirv *parsed_data,
const struct clc_spirv_specialization_consts *consts,
struct clc_binary *out_spirv)
{
if (!clc_spirv_specialize(in_spirv, parsed_data, consts, out_spirv))
return false;
if (debug_get_option_debug_clc() & CLC_DEBUG_DUMP_SPIRV)
clc_dump_spirv(out_spirv, stdout);
return true;
}
+229
View File
@@ -0,0 +1,229 @@
/*
* Copyright © Microsoft Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef MESA_CLC_H
#define MESA_CLC_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct nir_shader nir_shader;
struct nir_shader_compiler_options;
struct clc_named_value {
const char *name;
const char *value;
};
struct clc_compile_args {
const struct clc_named_value *headers;
unsigned num_headers;
struct clc_named_value source;
const char * const *args;
unsigned num_args;
};
struct clc_binary {
void *data;
size_t size;
};
struct clc_linker_args {
const struct clc_binary * const *in_objs;
unsigned num_in_objs;
unsigned create_library;
};
typedef void (*clc_msg_callback)(void *priv, const char *msg);
struct clc_logger {
void *priv;
clc_msg_callback error;
clc_msg_callback warning;
};
enum clc_kernel_arg_type_qualifier {
CLC_KERNEL_ARG_TYPE_CONST = 1 << 0,
CLC_KERNEL_ARG_TYPE_RESTRICT = 1 << 1,
CLC_KERNEL_ARG_TYPE_VOLATILE = 1 << 2,
};
enum clc_kernel_arg_access_qualifier {
CLC_KERNEL_ARG_ACCESS_READ = 1 << 0,
CLC_KERNEL_ARG_ACCESS_WRITE = 1 << 1,
};
enum clc_kernel_arg_address_qualifier {
CLC_KERNEL_ARG_ADDRESS_PRIVATE,
CLC_KERNEL_ARG_ADDRESS_CONSTANT,
CLC_KERNEL_ARG_ADDRESS_LOCAL,
CLC_KERNEL_ARG_ADDRESS_GLOBAL,
};
struct clc_kernel_arg {
const char *name;
const char *type_name;
unsigned type_qualifier;
unsigned access_qualifier;
enum clc_kernel_arg_address_qualifier address_qualifier;
};
enum clc_vec_hint_type {
CLC_VEC_HINT_TYPE_CHAR = 0,
CLC_VEC_HINT_TYPE_SHORT = 1,
CLC_VEC_HINT_TYPE_INT = 2,
CLC_VEC_HINT_TYPE_LONG = 3,
CLC_VEC_HINT_TYPE_HALF = 4,
CLC_VEC_HINT_TYPE_FLOAT = 5,
CLC_VEC_HINT_TYPE_DOUBLE = 6
};
struct clc_kernel_info {
const char *name;
size_t num_args;
const struct clc_kernel_arg *args;
unsigned vec_hint_size;
enum clc_vec_hint_type vec_hint_type;
};
enum clc_spec_constant_type {
CLC_SPEC_CONSTANT_UNKNOWN,
CLC_SPEC_CONSTANT_BOOL,
CLC_SPEC_CONSTANT_FLOAT,
CLC_SPEC_CONSTANT_DOUBLE,
CLC_SPEC_CONSTANT_INT8,
CLC_SPEC_CONSTANT_UINT8,
CLC_SPEC_CONSTANT_INT16,
CLC_SPEC_CONSTANT_UINT16,
CLC_SPEC_CONSTANT_INT32,
CLC_SPEC_CONSTANT_UINT32,
CLC_SPEC_CONSTANT_INT64,
CLC_SPEC_CONSTANT_UINT64,
};
struct clc_parsed_spec_constant {
uint32_t id;
enum clc_spec_constant_type type;
};
struct clc_parsed_spirv {
const struct clc_kernel_info *kernels;
unsigned num_kernels;
const struct clc_parsed_spec_constant *spec_constants;
unsigned num_spec_constants;
};
struct clc_libclc;
struct clc_libclc_options {
unsigned optimize;
const struct nir_shader_compiler_options *nir_options;
};
struct clc_libclc *clc_libclc_new(const struct clc_logger *logger, const struct clc_libclc_options *options);
void clc_free_libclc(struct clc_libclc *lib);
const nir_shader *clc_libclc_get_clc_shader(struct clc_libclc *lib);
void clc_libclc_serialize(struct clc_libclc *lib, void **serialized, size_t *size);
void clc_libclc_free_serialized(void *serialized);
struct clc_libclc *clc_libclc_deserialize(const void *serialized, size_t size);
bool
clc_compile_c_to_spir(const struct clc_compile_args *args,
const struct clc_logger *logger,
struct clc_binary *out_spir);
void
clc_free_spir(struct clc_binary *spir);
bool
clc_compile_spir_to_spirv(const struct clc_binary *in_spir,
const struct clc_logger *logger,
struct clc_binary *out_spirv);
void
clc_free_spirv(struct clc_binary *spirv);
bool
clc_compile_c_to_spirv(const struct clc_compile_args *args,
const struct clc_logger *logger,
struct clc_binary *out_spirv);
bool
clc_link_spirv(const struct clc_linker_args *args,
const struct clc_logger *logger,
struct clc_binary *out_spirv);
bool
clc_parse_spirv(const struct clc_binary *in_spirv,
const struct clc_logger *logger,
struct clc_parsed_spirv *out_data);
void
clc_free_parsed_spirv(struct clc_parsed_spirv *data);
typedef union {
bool b;
float f32;
double f64;
int8_t i8;
uint8_t u8;
int16_t i16;
uint16_t u16;
int32_t i32;
uint32_t u32;
int64_t i64;
uint64_t u64;
} clc_spirv_const_value;
struct clc_spirv_specialization {
uint32_t id;
clc_spirv_const_value value;
bool defined_on_module;
};
struct clc_spirv_specialization_consts {
const struct clc_spirv_specialization *specializations;
unsigned num_specializations;
};
bool
clc_specialize_spirv(const struct clc_binary *in_spirv,
const struct clc_parsed_spirv *parsed_data,
const struct clc_spirv_specialization_consts *consts,
struct clc_binary *out_spirv);
#ifdef __cplusplus
}
#endif
#endif /* MESA_CLC_H */
File diff suppressed because it is too large Load Diff
+104
View File
@@ -0,0 +1,104 @@
/*
* Copyright © Microsoft Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef MESA_CLC_HELPERS_H
#define MESA_CLC_HELPERS_H
#include "nir_types.h"
#include "clc.h"
#include "util/u_string.h"
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
bool
clc_spirv_get_kernels_info(const struct clc_binary *spvbin,
const struct clc_kernel_info **kernels,
unsigned *num_kernels,
const struct clc_parsed_spec_constant **spec_constants,
unsigned *num_spec_constants,
const struct clc_logger *logger);
void
clc_free_kernels_info(const struct clc_kernel_info *kernels,
unsigned num_kernels);
int
clc_c_to_spir(const struct clc_compile_args *args,
const struct clc_logger *logger,
struct clc_binary *out_spir);
int
clc_spir_to_spirv(const struct clc_binary *in_spir,
const struct clc_logger *logger,
struct clc_binary *out_spirv);
int
clc_c_to_spirv(const struct clc_compile_args *args,
const struct clc_logger *logger,
struct clc_binary *out_spirv);
int
clc_link_spirv_binaries(const struct clc_linker_args *args,
const struct clc_logger *logger,
struct clc_binary *out_spirv);
int
clc_spirv_specialize(const struct clc_binary *in_spirv,
const struct clc_parsed_spirv *parsed_data,
const struct clc_spirv_specialization_consts *consts,
struct clc_binary *out_spirv);
void
clc_dump_spirv(const struct clc_binary *spvbin, FILE *f);
void
clc_free_spir_binary(struct clc_binary *spir);
void
clc_free_spirv_binary(struct clc_binary *spvbin);
#define clc_log(logger, level, fmt, ...) do { \
if (!logger || !logger->level) break; \
char *_msg = NULL; \
asprintf(&_msg, fmt, ##__VA_ARGS__); \
assert(_msg); \
logger->level(logger->priv, _msg); \
free(_msg); \
} while (0)
#define clc_error(logger, fmt, ...) clc_log(logger, error, fmt, ##__VA_ARGS__)
#define clc_warning(logger, fmt, ...) clc_log(logger, warning, fmt, ##__VA_ARGS__)
#ifdef __cplusplus
}
#endif
#endif /* MESA_CLC_HELPERS_H */
+56
View File
@@ -0,0 +1,56 @@
# Copyright © Microsoft Corporation
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
clang_resource_dir = join_paths(llvm_libdir, 'clang', dep_llvm.version(), 'include')
opencl_c_h = custom_target(
'opencl-c.h',
input : [files_xxd, join_paths(clang_resource_dir, 'opencl-c.h')],
output : 'opencl-c.h.h',
command : [prog_python, '@INPUT@', '@OUTPUT@', '-n', 'opencl_c_source'],
)
opencl_c_base_h = custom_target(
'opencl-c-base.h',
input : [files_xxd, join_paths(clang_resource_dir, 'opencl-c-base.h')],
output : 'opencl-c-base.h.h',
command : [prog_python, '@INPUT@', '@OUTPUT@', '-n', 'opencl_c_base_source'],
)
files_libclc = files(
'clc.c',
'clc_helpers.cpp',
)
_libclc = static_library(
'libclc',
files_libclc,
opencl_c_h,
opencl_c_base_h,
include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_compiler, inc_spirv],
cpp_args : ['-DCLANG_RESOURCE_DIR="@0@"'.format(clang_resource_dir)],
dependencies: [idep_nir_headers, dep_clang, dep_llvm, dep_llvmspirvlib,
idep_mesautil, idep_nir, dep_spirv_tools]
)
idep_clc = declare_dependency(
link_with : _libclc,
include_directories : include_directories('.'),
)