nak/sm50: add a memstream abstraction

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/27158>
This commit is contained in:
Daniel Almeida 2024-01-12 20:59:32 -03:00 committed by Marge Bot
parent e57cf175e2
commit 02774be708
6 changed files with 106 additions and 0 deletions

View file

@ -39,6 +39,7 @@ libnak_c_files = files(
'nak_nir_lower_tex.c',
'nak_nir_lower_vtg_io.c',
'nak_nir_lower_gs_intrinsics.c',
'nak_memstream.c',
)
_libbitview_rs = static_library(

View file

@ -6,6 +6,7 @@
use crate::cfg::CFGBuilder;
use crate::ir::*;
use crate::nir::*;
use crate::nir_instr_printer::NirInstrPrinter;
use crate::sph::{OutputTopology, PixelImap};
use nak_bindings::*;
@ -239,6 +240,7 @@ struct ShaderFromNir<'a> {
end_block_id: u32,
ssa_map: HashMap<u32, Vec<SSAValue>>,
saturated: HashSet<*const nir_def>,
nir_instr_printer: NirInstrPrinter,
}
impl<'a> ShaderFromNir<'a> {
@ -255,6 +257,7 @@ impl<'a> ShaderFromNir<'a> {
end_block_id: 0,
ssa_map: HashMap::new(),
saturated: HashSet::new(),
nir_instr_printer: NirInstrPrinter::new(),
}
}

View file

@ -16,6 +16,7 @@ mod liveness;
mod lower_copy_swap;
mod lower_par_copies;
mod nir;
mod nir_instr_printer;
mod opt_bar_prop;
mod opt_copy_prop;
mod opt_dce;

View file

@ -0,0 +1,50 @@
// Copyright © 2024 Collabora, Ltd.
// SPDX-License-Identifier: MIT
use std::pin::Pin;
use nak_bindings::nak_clear_memstream;
use nak_bindings::nak_close_memstream;
use nak_bindings::nak_memstream;
use nak_bindings::nak_nir_asprint_instr;
use nak_bindings::nak_open_memstream;
use nak_bindings::nir_instr;
/// A memstream that holds the printed NIR instructions.
pub struct NirInstrPrinter {
// XXX: we need this to be pinned because we've passed references to its
// fields when calling open_memstream.
stream: Pin<Box<nak_memstream>>,
}
impl NirInstrPrinter {
pub fn new() -> Self {
let mut stream =
Box::pin(unsafe { std::mem::zeroed::<nak_memstream>() });
unsafe {
nak_open_memstream(stream.as_mut().get_unchecked_mut());
}
Self { stream }
}
/// Prints the given NIR instruction.
pub fn instr_to_string(&mut self, instr: &nir_instr) -> String {
unsafe {
let stream = self.stream.as_mut().get_unchecked_mut();
nak_nir_asprint_instr(stream, instr);
let c_str = std::ffi::CStr::from_ptr(stream.buffer);
let string = c_str.to_string_lossy().into_owned();
nak_clear_memstream(stream);
string
}
}
}
impl Drop for NirInstrPrinter {
fn drop(&mut self) {
unsafe {
let stream = self.stream.as_mut().get_unchecked_mut();
nak_close_memstream(stream)
}
}
}

View file

@ -0,0 +1,39 @@
/*
* Copyright © 2024 Collabora, Ltd.
* SPDX-License-Identifier: MIT
*
* This file exposes a nice interface that can be consumed from Rust. We would
* have to have Rust libc bindings otherwise.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "nak_private.h"
#include "nir.h"
void nak_open_memstream(struct nak_memstream *memstream)
{
memstream->stream = open_memstream(&memstream->buffer, &memstream->written);
fflush(memstream->stream);
assert(memstream->stream);
assert(memstream->buffer);
}
void nak_close_memstream(struct nak_memstream *memstream)
{
fclose(memstream->stream);
free(memstream->buffer);
}
void nak_nir_asprint_instr(struct nak_memstream *memstream, const nir_instr *instr)
{
nir_print_instr(instr, memstream->stream);
fflush(memstream->stream);
}
void nak_clear_memstream(struct nak_memstream *memstream)
{
rewind(memstream->stream);
}

View file

@ -205,6 +205,18 @@ bool nak_nir_add_barriers(nir_shader *nir, const struct nak_compiler *nak);
#define NAK_FS_OUT_COLOR(n) (NAK_FS_OUT_COLOR0 + (n) * 16)
struct nak_memstream {
FILE *stream;
char *buffer;
size_t written;
};
void nak_open_memstream(struct nak_memstream *memstream);
void nak_close_memstream(struct nak_memstream *memstream);
void nak_flush_memstream(struct nak_memstream *memstream);
void nak_clear_memstream(struct nak_memstream *memstream);
void nak_nir_asprint_instr(struct nak_memstream *memstream, const nir_instr *instr);
#ifdef __cplusplus
}
#endif