mesa/src/util/u_printf.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

140 lines
4.2 KiB
C
Raw Normal View History

//
// Copyright 2020 Serge Martin
//
// 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 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 U_PRINTF_H
#define U_PRINTF_H
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include "simple_mtx.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct u_printf_info {
unsigned num_args;
unsigned *arg_sizes;
unsigned string_size;
char *strings;
} u_printf_info;
struct blob;
struct blob_reader;
void u_printf_serialize_info(struct blob *blob,
const u_printf_info *info,
unsigned printf_info_count);
u_printf_info *u_printf_deserialize_info(void *mem_ctx,
struct blob_reader *blob,
unsigned *printf_info_count);
uint32_t u_printf_hash(const u_printf_info *info);
util/u_printf: add singleton implementation Currently, nir_lower_printf depends on a per-nir_shader table, writing out indices into the printf buffer. This works for real OpenCL implementations (rusticl, microsoft) which can associate the printf buffer with a particular kernel, I guess. (Actually it's not clear to me that it works well there either but that's not my problem.) This mechanism is unsuitable for internal driver shaders, where printfs with unique format strings can come from many different nir_shaders. There are two current solutions in tree to this for driver CL: * Honeykrisp: Only use one single nir_shader (libagx). This prevents us from using printf in common CL and requires extra driver tracking. It won't work with my upcoming vtn_bindgen rework, which is why I'm addressing this now. * Anv: Offset format-string indices by a dynamic "base identifier" using relocs or a push constant, then pool format strings into a table from nir_shader's across the device. The problem here is that these indices now depend on the order that nir_shaders are seen (which causes a mess for caching if relocs are used, or requires extra push constants and extra bookkeping if relocs aren't used). And the driver tracking required to do this pooling correctly is even more complicated than what Honeykrisp does. I do not want every driver in-tree needing to go down this path, and it wouldn't work with my upcoming vtn_bindgen. This MR introduces an alternate approach: rather than writing indices into the table, we instead hash the format string itself and write the hash. That doesn't depend on what nir_shader we came from, so we can freely mix & match and get consistent hashes. That greatly alleviates driver tracking burden. To make that possible, we need a global hash table mapping hashed format identifiers to the format strings themselves. That approach still requires a step to "register" format strings into the table. That step would not be required if we wrote the actual strings themselves into the table, but that was ruled out for performance/code size reasons. However, we do not want drivers to need to explicitly register all the strings they use, because once we have OpenCL in common code via vtn_bindgen2, drivers won't know all the strings they use. Fortunately, there's a neat solution for that too. By making this global table a singleton (with internal locking), vtn_bindgen2 can automatically register format strings via a static constructor. In conjunction with the infrastructure added here, that eliminates all driver bookkeeping required for format-strings. The code itself is inspired by the glsl type singleton. Is it pretty? Not really, but it gets the job done well. Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io> Reviewed-by: Jesse Natalie <jenatali@microsoft.com> Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/33067>
2025-01-15 15:40:20 -05:00
void u_printf_singleton_init_or_ref(void);
void u_printf_singleton_decref(void);
void u_printf_singleton_add(const u_printf_info *info, unsigned count);
void u_printf_singleton_add_serialized(const void *data, size_t data_size);
const u_printf_info *u_printf_singleton_search(uint32_t hash);
struct u_printf_ctx {
simple_mtx_t lock;
void *bo;
uint32_t *map;
};
const char *util_printf_prev_tok(const char *str);
/* find next valid printf specifier in a C string wrapper */
size_t util_printf_next_spec_pos(const char *str, size_t pos);
/* Return the length of the string that would be generated by a printf-style
* format and argument list, not including the \0 byte.
* The untouched_args parameter is left untouched so it can be re-used by the
* caller in a vsnprintf() call or similar.
*/
size_t u_printf_length(const char *fmt, va_list untouched_args);
void u_printf(FILE *out, const char *buffer, size_t buffer_size,
const u_printf_info*, unsigned info_size);
void u_printf_ptr(FILE *out, const char *buffer, size_t buffer_size,
const u_printf_info **info, unsigned info_size);
static inline void
u_printf_init(struct u_printf_ctx *ctx, void *bo, uint32_t *map)
{
ctx->bo = bo;
ctx->map = map;
simple_mtx_init(&ctx->lock, mtx_plain);
/* Initialize the buffer head to point to just after the size + abort word */
map[0] = 8;
/* Initially there is no abort */
map[1] = 0;
}
static inline void
u_printf_destroy(struct u_printf_ctx *ctx)
{
simple_mtx_destroy(&ctx->lock);
}
static inline void
u_printf_with_ctx(FILE *out, struct u_printf_ctx *ctx)
{
/* If the printf buffer is empty, early-exit without taking the lock. The
* speeds up the happy path and makes this function reasonable to call even
* in release builds.
*/
if (ctx->map[0] == 8)
return;
simple_mtx_lock(&ctx->lock);
u_printf(out, (char *)(ctx->map + 2), ctx->map[0] - 8, NULL, 0);
/* Reset */
ctx->map[0] = 8;
simple_mtx_unlock(&ctx->lock);
}
/*
* Flush the printf buffer and return whether there was an abort. This is
* intended to be called periodically to handle aborts in a timely manner.
*/
static inline bool
u_printf_check_abort(FILE *out, struct u_printf_ctx *ctx)
{
u_printf_with_ctx(out, ctx);
/* Check the aborted flag */
return (ctx->map[1] != 0);
}
#ifdef __cplusplus
}
#endif
#endif