Skip to content

[AutoDiff] Fix "'alignof' to an incomplete type" error in AutoDiff.h. #29983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2020

Conversation

dan-zheng
Copy link
Contributor

Using DenseMap<SILAutoDiffDerivativeFunctionKey, ...> in a file that includes
AutoDiff.h but not Types.h produced the following error:

swift-master/llvm-project/llvm/include/llvm/Support/PointerLikeTypeTraits.h:59:53: error: invalid application of 'alignof' to an incomplete type 'swift::SILFunctionType'
  enum { NumLowBitsAvailable = detail::ConstantLog2<alignof(T)>::value };
                                                    ^~~~~~~~~~
swift-master/llvm-project/llvm/include/llvm/ADT/DenseMapInfo.h:43:13: note: in instantiation of template class 'llvm::PointerLikeTypeTraits<swift::SILFunctionType *>' requested here
    Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
            ^
swift-master/swift/include/swift/AST/AutoDiff.h:383:46: note: in instantiation of member function 'llvm::DenseMapInfo<swift::SILFunctionType *>::getEmptyKey' requested here
    return {DenseMapInfo<SILFunctionType *>::getEmptyKey(),
                                             ^
swift-master/swift/include/swift/AST/AutoDiff.h:33:7: note: forward declaration of 'swift::SILFunctionType'
class SILFunctionType;
      ^

Fix the error by:

  • Including TypeAlignments.h in AutoDiff.h.
  • Adding an alignment forward declaration for SILFunctionType to TypeAlignments.h.

Resolves comment: https://github.com/apple/swift/pull/29953/files#r382372071.
No tests, since the error didn't surface in the current codebase.

I locally verified that the issue is fixed. The following changes didn't compile but now do:

diff --git a/lib/AST/AutoDiffTest.cpp b/lib/AST/AutoDiffTest.cpp
new file mode 100644
index 00000000000..ab1d4caa278
--- /dev/null
+++ b/lib/AST/AutoDiffTest.cpp
@@ -0,0 +1,9 @@
+#include "swift/AST/AutoDiff.h"
+#include "llvm/ADT/DenseMap.h"
+
+using namespace swift;
+using namespace autodiff;
+
+static void test() {
+  llvm::DenseMap<SILAutoDiffDerivativeFunctionKey, unsigned> map;
+}
diff --git a/lib/AST/CMakeLists.txt b/lib/AST/CMakeLists.txt
index 0add9c6606c..5c867797a28 100644
--- a/lib/AST/CMakeLists.txt
+++ b/lib/AST/CMakeLists.txt
@@ -27,6 +27,7 @@ add_swift_host_library(swiftAST STATIC
   ASTWalker.cpp
   Attr.cpp
   AutoDiff.cpp
+  AutoDiffTest.cpp
   Availability.cpp
   AvailabilitySpec.cpp
   Builtins.cpp

Using `DenseMap<SILAutoDiffDerivativeFunctionKey, ...>` in a file that includes
AutoDiff.h but not Types.h produced the following error:
```
error: invalid application of 'alignof' to an incomplete type 'swift::SILFunctionType'
```

Fix the error by:
- Including TypeAlignments.h in AutoDiff.h.
- Adding an alignment forward declaration for `SILFunctionType` to TypeAlignments.h.
@milseman
Copy link
Member

@swift-ci please smoke test

@milseman
Copy link
Member

Thank you!

@varungandhi-apple varungandhi-apple merged commit d7ab0c1 into swiftlang:master Feb 21, 2020
@dan-zheng dan-zheng deleted the autodiff-fix-alignof branch February 21, 2020 06:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants