This is an archive of the discontinued LLVM Phabricator instance.

[clang-cl] Add support for /kernel
ClosedPublic

Authored by steplong on May 31 2022, 1:50 PM.

Details

Summary

MSVC defines _KERNEL_MODE when /kernel is passed.
Also, /kernel disables RTTI and C++ exception handling.

https://docs.microsoft.com/en-us/cpp/build/reference/kernel-create-kernel-mode-binary?view=msvc-170

Diff Detail

Event Timeline

steplong created this revision.May 31 2022, 1:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 31 2022, 1:50 PM
steplong requested review of this revision.May 31 2022, 1:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 31 2022, 1:50 PM
steplong updated this revision to Diff 433185.May 31 2022, 2:04 PM
  • Test that exceptions aren't enabled with /kernel

https://docs.microsoft.com/en-us/cpp/build/reference/kernel-create-kernel-mode-binary?view=msvc-170 says we should err when /kernel is used with various flags (e.g. most /arch:) flags. Want to add that too?

It also says /kernel is passed on to the linker.

What does cl do if both /kernel and (eg) /EHsc is passed (try both orders)? Same question for /kernel /GR.

clang/lib/Driver/ToolChains/Clang.cpp
7459

hasFlag() is for when there's a positive (/kernel) and a negative (/kernel-) form. Is that the case here? Else, use hasArg instead.

What does cl do if both /kernel and (eg) /EHsc is passed (try both orders)? Same question for /kernel /GR.

From my experiments, cl doesn't complain and happily compiles the code if /kernel /EHsc is passed (both orders), unless the code uses exceptions. Then, it complains error C2980: C++ exception handling is not supported with /kernel (both orders)
For /GR, it complains Command line error D8016 : '/kernel' and '/GR' command-line options are incompatible

steplong updated this revision to Diff 433389.Jun 1 2022, 7:18 AM
  • Reject /kernel and /arch combinations for x64 and x86

It also says /kernel is passed on to the linker.

Do you know how the linker distinguishes objects created by /kernel vs non-/kernel? At the moment, we are just using MSVC's linker to link separately instead of being invoked by the compiler

thakis added inline comments.Jun 1 2022, 12:20 PM
clang/lib/Driver/ToolChains/Clang.cpp
7618

Making this list opt-in instead of opt-out (ie "SupportedArchs") seems a bit more future-proof: We're less likely to forget to update the list when new archs are added.

steplong updated this revision to Diff 433502.Jun 1 2022, 12:54 PM
  • Change UnsupportedArches to SupportedArches
thakis accepted this revision.Jun 3 2022, 8:31 AM

Looking pretty good!

clang/lib/Driver/ToolChains/Clang.cpp
7617

Just SupportedArches.push_back("IA32") seems a bit simpler.

(Or use append_range, but there's really no need)

clang/test/Driver/cl-zc.cpp
47

Tests for interaction of /kernel with /GR and /EH would be nice too, as mentioned.

This revision is now accepted and ready to land.Jun 3 2022, 8:31 AM
steplong updated this revision to Diff 434107.Jun 3 2022, 12:31 PM
  • Change to SupportedArches.insert()
  • Add logic that errors if /kernel and /GR
  • Add test for /kernel and /EHsc (silently override /EHsc. Only errors when trying to use exceptions in src code) and /kernel and /GR (fails with an error)
steplong updated this revision to Diff 434122.Jun 3 2022, 1:19 PM
  • Whoops missed a "/" in the test
steplong updated this revision to Diff 434459.Jun 6 2022, 6:55 AM
  • Clang-formatted patch
This revision was automatically updated to reflect the committed changes.