Make upb's generated code agnostic to fasttable.

This simplifies the code generation by making output agnostic to whether fasttables will be used or not.

This grows the generated code in the common case, but when fasttables are not being used the preprocessor will strip away the unused tables.

PiperOrigin-RevId: 499340805
pull/13171/head
Joshua Haberman 2 years ago committed by Copybara-Service
parent 7cd8f6c940
commit 143132fa27
  1. 4
      BUILD
  2. 28
      bazel/upb_proto_library.bzl
  3. 3
      cmake/make_cmakelists.py
  4. 4
      upb/port/def.inc
  5. 29
      upbc/protoc-gen-upb.cc

@ -31,11 +31,11 @@ load(
)
load(
"//bazel:upb_proto_library.bzl",
"upb_fasttable_enabled",
"upb_proto_library",
"upb_proto_library_copts",
"upb_proto_reflection_library",
)
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
# begin:google_only
# load(
@ -69,7 +69,7 @@ config_setting(
visibility = ["//visibility:public"],
)
upb_fasttable_enabled(
bool_flag(
name = "fasttable_enabled",
build_setting_default = False,
visibility = ["//visibility:public"],

@ -167,29 +167,6 @@ def _cc_library_func(ctx, name, hdrs, srcs, copts, includes, dep_ccinfos):
linking_context = linking_context,
)
# Build setting for whether fasttable code generation is enabled ###############
_FastTableEnabledInfo = provider(
"Provides fasttable configuration",
fields = {
"enabled": "whether fasttable is enabled",
},
)
def fasttable_enabled_impl(ctx):
raw_setting = ctx.build_setting_value
if raw_setting:
# TODO(haberman): check that the target CPU supports fasttable.
pass
return _FastTableEnabledInfo(enabled = raw_setting)
upb_fasttable_enabled = rule(
implementation = fasttable_enabled_impl,
build_setting = config.bool(flag = True),
)
# Dummy rule to expose select() copts to aspects ##############################
UpbProtoLibraryCoptsInfo = provider(
@ -235,9 +212,6 @@ def _compile_upb_protos(ctx, generator, proto_info, proto_sources):
srcs = [_generate_output_file(ctx, name, ext + ".c") for name in proto_sources]
hdrs = [_generate_output_file(ctx, name, ext + ".h") for name in proto_sources]
transitive_sets = proto_info.transitive_descriptor_sets.to_list()
fasttable_enabled = (hasattr(ctx.attr, "_fasttable_enabled") and
ctx.attr._fasttable_enabled[_FastTableEnabledInfo].enabled)
codegen_params = "fasttable:" if fasttable_enabled else ""
ctx.actions.run(
inputs = depset(
direct = [proto_info.direct_descriptor_set],
@ -247,7 +221,7 @@ def _compile_upb_protos(ctx, generator, proto_info, proto_sources):
outputs = srcs + hdrs,
executable = ctx.executable._protoc,
arguments = [
"--" + generator + "_out=" + codegen_params + _get_real_root(ctx, srcs[0]),
"--" + generator + "_out=" + _get_real_root(ctx, srcs[0]),
"--plugin=protoc-gen-" + generator + "=" + tool.path,
"--descriptor_set_in=" + ctx.configuration.host_path_separator.join([f.path for f in transitive_sets]),
] +

@ -197,6 +197,9 @@ class BuildFileFunctions(object):
def package_group(self, **kwargs):
pass
def bool_flag(self, **kwargs):
pass
class WorkspaceFileFunctions(object):
def __init__(self, converter):

@ -243,8 +243,8 @@
#endif
/* UPB_FASTTABLE_INIT() allows protos compiled for fasttable to gracefully
* degrade to non-fasttable if we are using UPB_TRY_ENABLE_FASTTABLE. */
#if !UPB_FASTTABLE && defined(UPB_TRY_ENABLE_FASTTABLE)
* degrade to non-fasttable if the runtime or platform do not support it. */
#if !UPB_FASTTABLE
#define UPB_FASTTABLE_INIT(...)
#else
#define UPB_FASTTABLE_INIT(...) __VA_ARGS__

@ -1182,7 +1182,7 @@ void WriteMessageField(const upb_MiniTableField* field64,
// Writes a single message into a .upb.c source file.
void WriteMessage(const protobuf::Descriptor* message, const FileLayout& layout,
Output& output, bool fasttable_enabled) {
Output& output) {
std::string msg_name = ToCIdent(message->full_name());
std::string fields_array_ref = "NULL";
std::string submsgs_array_ref = "NULL";
@ -1225,9 +1225,7 @@ void WriteMessage(const protobuf::Descriptor* message, const FileLayout& layout,
std::vector<TableEntry> table;
uint8_t table_mask = -1;
if (fasttable_enabled) {
table = FastDecodeTable(message, layout);
}
table = FastDecodeTable(message, layout);
if (table.size() > 1) {
assert((table.size() & (table.size() - 1)) == 0);
@ -1256,7 +1254,7 @@ void WriteMessage(const protobuf::Descriptor* message, const FileLayout& layout,
output(" {0x$1, &$0},\n", ent.first,
absl::StrCat(absl::Hex(ent.second, absl::kZeroPad16)));
}
output(" }),\n");
output(" })\n");
}
output("};\n\n");
}
@ -1310,15 +1308,14 @@ int WriteEnums(const FileLayout& layout, Output& output) {
return this_file_enums.size();
}
int WriteMessages(const FileLayout& layout, Output& output,
bool fasttable_enabled) {
int WriteMessages(const FileLayout& layout, Output& output) {
const protobuf::FileDescriptor* file = layout.descriptor();
std::vector<const protobuf::Descriptor*> file_messages = SortedMessages(file);
if (file_messages.empty()) return 0;
for (auto message : file_messages) {
WriteMessage(message, layout, output, fasttable_enabled);
WriteMessage(message, layout, output);
}
output("static const upb_MiniTable *$0[$1] = {\n", kMessagesInit,
@ -1383,8 +1380,7 @@ int WriteExtensions(const FileLayout& layout, Output& output) {
}
// Writes a .upb.cc source file.
void WriteSource(const FileLayout& layout, Output& output,
bool fasttable_enabled) {
void WriteSource(const FileLayout& layout, Output& output) {
const protobuf::FileDescriptor* file = layout.descriptor();
EmitFileWarning(file, output);
@ -1405,7 +1401,7 @@ void WriteSource(const FileLayout& layout, Output& output,
"#include \"upb/port/def.inc\"\n"
"\n");
int msg_count = WriteMessages(layout, output, fasttable_enabled);
int msg_count = WriteMessages(layout, output);
int ext_count = WriteExtensions(layout, output);
int enum_count = WriteEnums(layout, output);
@ -1436,17 +1432,12 @@ bool Generator::Generate(const protobuf::FileDescriptor* file,
const std::string& parameter,
protoc::GeneratorContext* context,
std::string* error) const {
bool fasttable_enabled = false;
std::vector<std::pair<std::string, std::string>> params;
google::protobuf::compiler::ParseGeneratorParameter(parameter, &params);
for (const auto& pair : params) {
if (pair.first == "fasttable") {
fasttable_enabled = true;
} else {
*error = "Unknown parameter: " + pair.first;
return false;
}
*error = "Unknown parameter: " + pair.first;
return false;
}
FileLayout layout(file);
@ -1459,7 +1450,7 @@ bool Generator::Generate(const protobuf::FileDescriptor* file,
std::unique_ptr<protobuf::io::ZeroCopyOutputStream> c_output_stream(
context->Open(SourceFilename(file)));
Output c_output(c_output_stream.get());
WriteSource(layout, c_output, fasttable_enabled);
WriteSource(layout, c_output);
return true;
}

Loading…
Cancel
Save