# How to link one witness table from stdlib

**URL:** <https://forums.swift.org/t/how-to-link-one-witness-table-from-stdlib/12674>\
**Category:** Compiler\
**Created:** [May 14, 2018, 10:19pm UTC](https://forums.swift.org/t/how-to-link-one-witness-table-from-stdlib/12674 "2018-05-14T22:19:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rxwei](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rxwei/32/664_2.png) [@rxwei](https://forums.swift.org/u/rxwei)\
**Post date:** [May 14, 2018, 10:19pm UTC](https://forums.swift.org/t/how-to-link-one-witness-table-from-stdlib/12674/1 "2018-05-14T22:19:25Z")

</div>

In a SIL pass, if I want to emit a call to a method required by a stdlib protocol using `witness_method` + `apply`, e.g. `FloatingPoint.init(_builtinIntegerLiteral:)`, how can I link just one witness table from the stdlib module?

Currently, only `SILModule::linkAllWitnessTables` works for me, but it's very inefficient.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [May 15, 2018, 3:56am UTC](https://forums.swift.org/t/how-to-link-one-witness-table-from-stdlib/12674/2 "2018-05-15T03:56:10Z")

</div>

It may be that there's no way to do that right now, but that ought to be relatively easy to fix.

---

<div class="post-metadata">

**Author:** ![rxwei](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rxwei/32/664_2.png) [@rxwei](https://forums.swift.org/u/rxwei)\
**Post date:** [May 15, 2018, 7:45am UTC](https://forums.swift.org/t/how-to-link-one-witness-table-from-stdlib/12674/3 "2018-05-15T07:45:55Z")

</div>

Trying to directly use `SerializedSILLoader` now. Thanks.

---

<div class="post-metadata">

**Author:** ![rxwei](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rxwei/32/664_2.png) [@rxwei](https://forums.swift.org/u/rxwei)\
**Post date:** [May 16, 2018, 11:23pm UTC](https://forums.swift.org/t/how-to-link-one-witness-table-from-stdlib/12674/4 "2018-05-16T23:23:29Z")

</div>

I ended up having something like:

```cpp
    auto *decl = conf->getDeclContext()->getAsNominalTypeOrNominalTypeExtensionContext();
    auto linkage = getSILLinkage(getDeclLinkage(decl), NotForDefinition);
    auto *newTable = module.createWitnessTableDeclaration(conf, linkage);
    witnessTables.insert({conf, newTable});
    newTable = silLoader->lookupWitnessTable(newTable);

```

However, the witness functions this declares have `shared` linkage by default. This causes the verifier to crash because the witness tables are `public_external`. **Not sure if it's a bug or not.**

To work around the crasher, I reassigned `public_external` linkage to each witness.

```cpp
    // Update linkage for witness methods.
    // FIXME: Figure out why witnesses have shared linkage by default.
    for (auto &entry : newTable->getEntries())
      if (entry.getKind() == SILWitnessTable::WitnessKind::Method)
        entry.getMethodWitness().Witness->setLinkage(linkage);

```

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [May 17, 2018, 3:42am UTC](https://forums.swift.org/t/how-to-link-one-witness-table-from-stdlib/12674/5 "2018-05-17T03:42:59Z")

</div>

You don't need to deserialize a SIL witness table to emit a `witness_method` instruction. The `witness_method` instruction references a type (which can also be concrete) and the protocol requirement's `ValueDecl` wrapped in a `SILDeclRef`. The devirtualizer pass can deserialize witness tables already when transforming witness\_method calls to concrete function references. Also the SIL linker will automatically deserialize witness tables as needed. I don't think you should be attempting to devirtualize such method calls yourself.

Can you give more details about what you are trying to do?

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [May 17, 2018, 3:43am UTC](https://forums.swift.org/t/how-to-link-one-witness-table-from-stdlib/12674/6 "2018-05-17T03:43:56Z")

</div>

> [@rxwei](#):
>
> To work around the crasher, I reassigned `public_external` linkage to each witness.

Are the witnesses themselves public? SILGen emits witness thunks with shared linkage, which means you need their body if you're planning on referencing them directly, so this simple approach won't work and you should instead use the SIL linker.

However, see my other e-mail in this thread; I suspect you don't need to explicitly deserialize a witness table at all.

---

<div class="post-metadata">

**Author:** ![rxwei](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rxwei/32/664_2.png) [@rxwei](https://forums.swift.org/u/rxwei)\
**Post date:** [May 29, 2018, 6:32pm UTC](https://forums.swift.org/t/how-to-link-one-witness-table-from-stdlib/12674/7 "2018-05-29T18:32:28Z")

</div>

Thanks for the pointers. Perhaps there's a bug in `SILModule::lookUpWitnessTable` on the tensorflow branch, where the `derializeLazily` flag won't make it successfully serialize the witness table.

The witnesses themselves are public: I'm calling `_ExpressibleByBuiltinIntegerLiteral::init(_builtinIntegerLiteral:)`. Currently `SerializedSILLoader` works perfectly. I'm yet to verify whether the simpler approach works on master yet.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [June 8, 2018, 6:55am UTC](https://forums.swift.org/t/how-to-link-one-witness-table-from-stdlib/12674/8 "2018-06-08T06:55:17Z")

</div>

> [@rxwei](#):
>
> The witnesses themselves are public:

Even if a witness is public, the witness thunk is not going to be public. Witnesses are always invoked through a witness thunk since protocol requirements use a special ABI distinct from non-protocol methods.

> [@rxwei](#):
>
> I'm calling `_ExpressibleByBuiltinIntegerLiteral::init(_builtinIntegerLiteral:)` . Currently `SerializedSILLoader` works perfectly. I'm yet to verify whether the simpler approach works on master yet.

Why don't you instead emit a `witness_method` instruction to call the protocol requirement and allow the devirtualizer to turn it into a call of a concrete function later? I don't think it's a good idea for several parts of the compiler to perform devirtualization.

---

<div class="post-metadata">

**Author:** ![rxwei](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rxwei/32/664_2.png) [@rxwei](https://forums.swift.org/u/rxwei)\
**Post date:** [June 8, 2018, 4:51pm UTC](https://forums.swift.org/t/how-to-link-one-witness-table-from-stdlib/12674/9 "2018-06-08T16:51:35Z")

</div>

> Why don't you instead emit a witness\_method instruction to call the protocol requirement and allow the devirtualizer to turn it into a call of a concrete function later?

That's what I tried initially, but it either crashed or failed verification (I can't remember). It's likely an issue in the current _tensorflow branch_, so I had to use the deserializer. I can verify later when we merge from upstream.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [June 9, 2018, 7:03am UTC](https://forums.swift.org/t/how-to-link-one-witness-table-from-stdlib/12674/10 "2018-06-09T07:03:03Z")

</div>

> [@rxwei](#):
>
> That's what I tried initially, but it either crashed or failed verification (I can't remember). It's likely an issue in the current _tensorflow branch_ , so I had to use the deserializer. I can verify later when we merge from upstream.

Please do. In general, if you come across some behavior that doesn't make sense and requires a hack to work around, make a note of it at least, so that we can sort out the underlying issue before the tensorflow work goes upstream.

Also there's a general strategy that works well for asking questions: instead of asking how you can do some specific thing, tell us what the overall problem you're trying to solve is. In this case, it would be the devirtualization crash. Manually deserializing a witness table is just a workaround that obscures what was really going on.
