# External function declaration has changed. Help needed

**URL:** <https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357>\
**Category:** MLIR\
**Created:** [December 8, 2020, 4:04pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357 "2020-12-08T16:04:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpotop](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/dpotop/32/324_2.png) [@dpotop](https://discourse.llvm.org/u/dpotop)\
**Post date:** [December 8, 2020, 4:04pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/1 "2020-12-08T16:04:03Z")

</div>

Hello,

It seems that the function declaration syntax has changed, and that each declaration of an external function must be assigned to a module.

Can you point me to the relevant examples or documentation?

The two cases I’m interested in are:

- Function @f declared in C file abc.c. What is the module name?
- Function @f declared in mlir file def.mlir. What is the module name?

BTW, what is the reason for no longer allowing function declarations with global visibility?

Best,  
Dumitru

---

<div class="post-metadata">

**Author:** ![dpotop](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/dpotop/32/324_2.png) [@dpotop](https://discourse.llvm.org/u/dpotop)\
**Post date:** [December 8, 2020, 4:25pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/2 "2020-12-08T16:25:18Z")

</div>

Ok, after some research it seems that I have to declare all declarations of functions as “private”. But is this really necessary? Isn’t this implicit, that declarations only have the scope of the current module?

---

<div class="post-metadata">

**Author:** ![clattner](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/clattner/32/187_2.png) [@clattner](https://discourse.llvm.org/u/clattner)\
**Post date:** [December 8, 2020, 5:36pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/3 "2020-12-08T17:36:38Z")

</div>

This also seems weird to me. Generally we want the default to work without syntactic overhead like this.

---

<div class="post-metadata">

**Author:** ![mehdi\_amini](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/mehdi_amini/32/25595_2.png) [@mehdi\_amini](https://discourse.llvm.org/u/mehdi_amini)\
**Post date:** [December 8, 2020, 5:54pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/4 "2020-12-08T17:54:00Z")

</div>

This was discussed here: [RFC: Symbol definition/declaration x visibility checks - #11 by jurahul](http://discourse.llvm.org/t/rfc-symbol-definition-declaration-x-visibility-checks/2140/11)

I also suggested this in the review and @jurahul mentioned that we can add this later after another issue is fixed in the parser (it has been fixed since then): [https://reviews.llvm.org/D91456#inline-851955](https://reviews.llvm.org/D91456#inline-851955)

---

<div class="post-metadata">

**Author:** ![jurahul](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/jurahul/32/16923_2.png) [@jurahul](https://discourse.llvm.org/u/jurahul)\
**Post date:** [December 8, 2020, 8:02pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/5 "2020-12-08T20:02:37Z")

</div>

Yes, inferring the default required fixing FuncOp parsing to reject empty regions, which has landed last week ([https://reviews.llvm.org/D91886](https://reviews.llvm.org/D91886)). I will now work on changing the FuncOp parser to infer default private visibility for function declarations so that an explicit “private” is not needed.

---

<div class="post-metadata">

**Author:** ![jurahul](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/jurahul/32/16923_2.png) [@jurahul](https://discourse.llvm.org/u/jurahul)\
**Post date:** [December 8, 2020, 9:55pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/6 "2020-12-08T21:55:36Z")

</div>

Quick question: Do we want the printer to also hide “private” when the function being printed is a declaration and hence has a default private visibility? This will cause a lot of churn in tests, since CHECK’s have to be updated. That is fine, but just want to get a consensus before I update a large number of tests. So a private function declaration can come in 3 forms:

```auto
func @foo() // private inferred
func private @foo() // explicit visibility
func @foo() attributes { sym_visibility = "private" } // legacy syntax

```

and in all cases will be printed as

```auto
func @foo()

```

Does that seems reasonable? Note that if it was not a declaration:

```auto
func @foo() { 
  return
}

```

The default visibility is public. Is that a cause of concern?

---

<div class="post-metadata">

**Author:** ![mehdi\_amini](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/mehdi_amini/32/25595_2.png) [@mehdi\_amini](https://discourse.llvm.org/u/mehdi_amini)\
**Post date:** [December 8, 2020, 11:02pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/7 "2020-12-08T23:02:45Z")

</div>

I’d be in favor of leaving it out (implicit) for declarations, but @ftynse had concerns about it so I’d like to hear from him first.

---

<div class="post-metadata">

**Author:** ![nicolasvasilache](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/nicolasvasilache/32/131_2.png) [@nicolasvasilache](https://discourse.llvm.org/u/nicolasvasilache)\
**Post date:** [December 9, 2020, 2:13pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/8 "2020-12-09T14:13:59Z")

</div>

It would be nice if it roundtripped without modification in both the implicit and explicit private cases.

---

<div class="post-metadata">

**Author:** ![jpienaar](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/jpienaar/32/225_2.png) [@jpienaar](https://discourse.llvm.org/u/jpienaar)\
**Post date:** [December 9, 2020, 3:22pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/9 "2020-12-09T15:22:31Z")

</div>

> [@nicolasvasilache](#):
>
> roundtripped without modification

That would require one to record and track how it was parsed through to printing. Given textual form is for debugging, I don’t know if making that tracked in IR is good tradeoff.

I’d be pro not printing if default, matches other default printing behavior. I believe the generic form always would print, but the pretty form only optionally.

> [@jurahul](#):
>
> default visibility is public. Is that a cause of concern?

That would be confusing to me: I’d expect same default visibility for both. I would not consider that declaration and definition have different default visibility.

---

<div class="post-metadata">

**Author:** ![nicolasvasilache](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/nicolasvasilache/32/131_2.png) [@nicolasvasilache](https://discourse.llvm.org/u/nicolasvasilache)\
**Post date:** [December 9, 2020, 3:26pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/10 "2020-12-09T15:26:40Z")

</div>

> [@jpienaar](#):
>
> That would require one to record and track how it was parsed through to printing. Given textual form is for debugging, I don’t know if making that tracked in IR is good tradeoff.

I would have expected that the underlying representation / attribute accepts a null / empty value and interprets that as private ?

---

<div class="post-metadata">

**Author:** ![bondhugula](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@bondhugula](https://discourse.llvm.org/u/bondhugula)\
**Post date:** [December 9, 2020, 3:27pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/11 "2020-12-09T15:27:47Z")

</div>

It’s really annoying and weird to have to keep adding `private` for all external function declarations - I really can’t see what rationale would justify this well.

---

<div class="post-metadata">

**Author:** ![mehdi\_amini](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/mehdi_amini/32/25595_2.png) [@mehdi\_amini](https://discourse.llvm.org/u/mehdi_amini)\
**Post date:** [December 9, 2020, 4:51pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/12 "2020-12-09T16:51:11Z")

</div>

The rational to add it would be to be explicit about what the declaration means and be closer to the in-memory representation, i.e. not having:

```auto
func @foo() // implicitly private

func @bar() { // implicitly public
  ...
}

```

The alternative could be to make private the default in both cases and require an explicit `public` for function definitions (but that would break many users right now).

---

<div class="post-metadata">

**Author:** ![mehdi\_amini](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/mehdi_amini/32/25595_2.png) [@mehdi\_amini](https://discourse.llvm.org/u/mehdi_amini)\
**Post date:** [December 9, 2020, 4:54pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/13 "2020-12-09T16:54:55Z")

</div>

> [@nicolasvasilache](#):
>
> I would have expected that the underlying representation / attribute accepts a null / empty value and interprets that as private ?

There is no such mechanism, and it’d be anti-canonicalization to have to keep two in-memory form for the same semantics. Doing this to preserve the round-trip of the custom printer does not seem usual to me, have we done this somewhere already?

---

<div class="post-metadata">

**Author:** ![jurahul](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/jurahul/32/16923_2.png) [@jurahul](https://discourse.llvm.org/u/jurahul)\
**Post date:** [December 9, 2020, 4:57pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/14 "2020-12-09T16:57:53Z")

</div>

So I guess there are 2 issues to sort through:

1. Should default visibility be same or different between declarations and definitions?

2. Should an explicit private be roundtrippable?

That could be done with existing attributes (the single `sym_visibility` attribute) if we change the `SymbolOpInterface::getVisibility` to allow operations to define their default visibility if the attribute is not present. So:

```auto
   InterfaceMethod<"Gets the visibility of this symbol.",
      "mlir::SymbolTable::Visibility", "getVisibility", (ins), [{}],
      /*defaultImplementation=*/[{
        return mlir::SymbolTable::getSymbolVisibility(this->getOperation());
      }]
    >,

```

will change to something like:

```auto
   InterfaceMethod<"Gets the visibility of this symbol.",
      "mlir::SymbolTable::Visibility", "getVisibility", (ins), [{}],
      /*defaultImplementation=*/[{
        return mlir::SymbolTable::getSymbolVisibility(this->getOperation(), getDefaultVisibility());
      }]
    >,

```

where `getDefaultVisibility()` will be a new `SymbolOpInterface` method that will determine the inferred visibility of a given instance of a Symbol operation if there is no visibility attribute. Its default implementation will return “public” to match the existing behavior.

---

<div class="post-metadata">

**Author:** ![nicolasvasilache](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/nicolasvasilache/32/131_2.png) [@nicolasvasilache](https://discourse.llvm.org/u/nicolasvasilache)\
**Post date:** [December 9, 2020, 5:45pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/15 "2020-12-09T17:45:24Z")

</div>

Not sure what you mean: there is such a thing as “OptionalAttr”, I see GlobalMemRefOp using that in “`std`”. I’d expect the absence of that attr to not print anything.

---

<div class="post-metadata">

**Author:** ![jurahul](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/jurahul/32/16923_2.png) [@jurahul](https://discourse.llvm.org/u/jurahul)\
**Post date:** [December 9, 2020, 5:49pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/16 "2020-12-09T17:49:44Z")

</div>

Yes, `sym_visibility` is essentially that optional attribute. We just need to parameterize the default visibility value when this optional attribute is absent to get the desired round trip behavior.

---

<div class="post-metadata">

**Author:** ![mehdi\_amini](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/mehdi_amini/32/25595_2.png) [@mehdi\_amini](https://discourse.llvm.org/u/mehdi_amini)\
**Post date:** [December 9, 2020, 5:52pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/17 "2020-12-09T17:52:29Z")

</div>

@nicolasvasilache Rahul explained above, the absence of the attribute implies “public”: this is not something we can really specialized based on declaration vs definition.

Assuming the custom parser for function declaration is changed to automatically set the attribute to private, the two declarations are parsed the same way:

```auto
func @foo()
func private @bar()

```

On printing we can print or omit the “private”, but we have to pick one. If you want to roundtrip these _syntactically_ (and not just semantically) we need to know if the “private” was explicitly specified in the text and this information can’t be preserved without an extra attribute (this is what @jpienaar was describing above I believe)

---

<div class="post-metadata">

**Author:** ![jurahul](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/jurahul/32/16923_2.png) [@jurahul](https://discourse.llvm.org/u/jurahul)\
**Post date:** [December 9, 2020, 5:54pm UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/18 "2020-12-09T17:54:49Z")

</div>

> [@mehdi\_amini](#):
>
> this is not something we can really specialized based on declaration vs definition.

I am suggesting we do this exactly in the `SymbolOpInterface`. Let instances of operations define what their default/inferred visibility is when the optional `sym_visibility` attribute is absent and then we can get round trip behavior where explicit “private” is printed as “private”…

---

<div class="post-metadata">

**Author:** ![jurahul](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/jurahul/32/16923_2.png) [@jurahul](https://discourse.llvm.org/u/jurahul)\
**Post date:** [December 10, 2020, 3:12am UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/19 "2020-12-10T03:12:00Z")

</div>

Collecting the feedback above, here’s my proposal:

1. We need to agree that if we want visibility to be inferred for functions (and similar rules would  
probably apply for other symbols), then

2. `Symbol` operations already need to say whether its a declaration or definition. We will define a symbols default visibility to be public if its a definition and private if its a declaration.

3. A `Symbol` operation can then be in either of the 2 states as follows:

Please let me know if this proposal works.

---

<div class="post-metadata">

**Author:** ![mehdi\_amini](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/mehdi_amini/32/25595_2.png) [@mehdi\_amini](https://discourse.llvm.org/u/mehdi_amini)\
**Post date:** [December 10, 2020, 5:28am UTC](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357/20 "2020-12-10T05:28:33Z")

</div>

Looks good to me! Thanks 🙂

[Next page](https://discourse.llvm.org/t/external-function-declaration-has-changed-help-needed/2357.md?page=2)
