Skip to content

Commit 68a6e46

Browse files
ezyangfacebook-github-bot
authored andcommitted
Push anonymous namespace into codegen, not template (#49498)
Summary: Pull Request resolved: #49498 In the near future, I want to code generate some functions that are visible externally to this compilation unit. I cannot easily do this if all the codegen code is wrapped in a global anonymous namespace, so push the namespace in. Registration has to stay in an anonymous namespace to avoid name conflicts. This could also have been solved by making the wrapper functions have more unique names but I didn't do this in the end. Signed-off-by: Edward Z. Yang <ezyang@fb.com> Test Plan: Imported from OSS Reviewed By: albanD, smessmer Differential Revision: D25616104 Pulled By: ezyang fbshipit-source-id: 323c0dda05a081502aab702f359a08dfac8c41a4
1 parent 480a756 commit 68a6e46

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

aten/src/ATen/templates/RegisterDispatchKey.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@
3737

3838
namespace at {
3939

40-
namespace {
41-
4240
${dispatch_definitions}
4341

42+
// NB: TORCH_LIBRARY_IMPL must be in an anonymous namespace to avoid
43+
// ambiguity with conflicting identifiers that may have been defined in
44+
// at namespace already.
45+
namespace {
46+
4447
TORCH_LIBRARY_IMPL(aten, ${DispatchKey}, m) {
4548
${dispatch_registrations}
4649
}

tools/codegen/gen.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,8 @@ def gen_one(f: NativeFunction) -> Optional[str]:
435435
# For an overview of what this template code looks like, see
436436
# https://github.com/pytorch/rfcs/pull/9
437437
return f"""\
438+
namespace {{
439+
438440
{self.gen_structured_class(
439441
f, k,
440442
class_name=class_name,
@@ -448,6 +450,8 @@ def gen_one(f: NativeFunction) -> Optional[str]:
448450
{impl_call}
449451
return {ret_expr};
450452
}}
453+
454+
}} // anonymous namespace
451455
"""
452456

453457
elif self.target is Target.REGISTRATION:
@@ -540,9 +544,13 @@ def gen_unstructured(self, f: NativeFunction) -> Optional[str]:
540544
"""
541545

542546
return f"""\
547+
namespace {{
548+
543549
{returns_type} {name}({args_str}) {{
544550
{cuda_guard}{return_kw}{impl_name}({args_exprs_str});
545551
}}
552+
553+
}} // anonymous namespace
546554
"""
547555

548556
elif self.target is Target.REGISTRATION:

0 commit comments

Comments
 (0)