================ @@ -0,0 +1,344 @@ +//===-- CIRDialectBytecode.td - CIR bytecode defs ----------*- tablegen -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This is the CIR bytecode reader/writer definition file. +// +// Modelled on mlir/include/mlir/Dialect/LLVMIR/LLVMDialectBytecode.td. Every +// entry's parameters must appear in the same order as the attribute's get(), +// because the generated reader feeds them positionally, and each name must +// derive the C++ getter (`get` + the name in UpperCamel). +// +//===----------------------------------------------------------------------===// + +#ifndef CIR_DIALECT_BYTECODE +#define CIR_DIALECT_BYTECODE + +include "mlir/IR/BytecodeBase.td" + +//===----------------------------------------------------------------------===// +// Bytecode classes for attributes and types. +//===----------------------------------------------------------------------===// + +class Attr<string type> : WithType<type, Attribute>; + +// Read a generic type into a narrowed C++ handle. The reader's templated +// readType does the dyn_cast and fails cleanly on a mismatch, so this stays +// safe against a malformed or cross-version file. +class Ty<string type> : WithType<type, Type>; + +class OptionalAttribute<string type> : + WithParser <"succeeded($_reader.readOptionalAttribute($_var))", + WithPrinter<"$_writer.writeOptionalAttribute($_getter)", + WithType<type, Attribute>>>; + +// For a parameter declared OptionalParameter<"std::optional<...>">, where the +// C++ type is an optional rather than a nullable attribute, so "absent" needs +// a flag of its own. The helpers live in CIRDialectBytecode.cpp. +class OptionalInt<string type> : + WithParser <"succeeded(readOptionalInt($_reader, $_var))", + WithBuilder<"$_args", + WithPrinter<"writeOptionalInt($_writer, $_getter)", + WithType <"std::optional<" # type # ">">>>>; + +// Named for the C++ spelling, not the concept, because the difference from +// OptionalAttribute above is exactly the C++ spelling and nothing else: +// OptionalAttribute is for a parameter that is a nullable attribute, this is +// for one declared std::optional<...>. The two share the same wire format, so +// at a glance nothing but the name says which a given parameter needs. +class StdOptionalAttribute<string type> : + WithParser <"succeeded(readStdOptionalAttribute($_reader, $_var))", + WithBuilder<"$_args", + WithPrinter<"writeStdOptionalAttribute($_writer, $_getter)", + WithType <"std::optional<" # type # ">">>>>; + +// A scoped enum read from an untrusted varint. The value is validated with +// the enum's symbolizer; an out-of-range value fails the read with a +// diagnostic instead of casting into undefined behavior. +def CtorKindParam : + WithParser<"succeeded(readEnum($_reader, $_var, cir::symbolizeCtorKind, \"CtorKind\"))", + WithPrinter<"$_writer.writeVarInt(static_cast<uint64_t>($_getter))", + WithType<"cir::CtorKind">>>; + +def AssignKindParam : ---------------- xlauko wrote:
I would even consider adding this to core `BytecodeBase.td` as a generic util? https://github.com/llvm/llvm-project/pull/229586 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
