[PATCH] D57829: [CUDA][HIP] Disable emitting llvm.linker.options in device compilation

2019-11-04 Thread Yaxun Liu via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG4264e7bbfdb3: [CUDA][HIP] Disable emitting 
llvm.linker.options in device compilation (authored by yaxunl).
Herald added a project: clang.

Changed prior to commit:
  https://reviews.llvm.org/D57829?vs=227636=227816#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D57829/new/

https://reviews.llvm.org/D57829

Files:
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/CodeGenCUDA/ms-linker-options.cu
  clang/test/Driver/hip-autolink.hip


Index: clang/test/Driver/hip-autolink.hip
===
--- /dev/null
+++ clang/test/Driver/hip-autolink.hip
@@ -0,0 +1,14 @@
+// REQUIRES: clang-driver
+// REQUIRES: x86-registered-target
+// REQUIRES: amdgpu-registered-target
+//
+// RUN: %clang --target=i386-pc-windows-msvc --cuda-gpu-arch=gfx906 -nogpulib \
+// RUN:   --cuda-device-only -x hip %s -### 2>&1 | FileCheck 
--check-prefix=DEV %s
+// RUN: %clang --target=i386-pc-windows-msvc --cuda-gpu-arch=gfx906 -nogpulib \
+// RUN:   --cuda-host-only -x hip %s -### 2>&1 | FileCheck --check-prefix=HOST 
%s
+
+// DEV: "-cc1" "-triple" "amdgcn-amd-amdhsa"
+// DEV-SAME: "-fno-autolink"
+
+// HOST: "-cc1" "-triple" "i386-pc-windows-msvc{{.*}}"
+// HOST-NOT: "-fno-autolink"
Index: clang/test/CodeGenCUDA/ms-linker-options.cu
===
--- /dev/null
+++ clang/test/CodeGenCUDA/ms-linker-options.cu
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -emit-llvm -o - -fcuda-is-device -fms-extensions -x hip %s \
+// RUN:   -fno-autolink -triple amdgcn-amd-amdhsa \
+// RUN:   | FileCheck -check-prefix=DEV %s
+// RUN: %clang_cc1 -emit-llvm -o - -fms-extensions -x hip %s -triple \
+// RUN:x86_64-pc-windows-msvc | FileCheck -check-prefix=HOST %s
+// RUN: %clang_cc1 -emit-llvm -o - -fcuda-is-device -fms-extensions %s \
+// RUN:   -fno-autolink -triple amdgcn-amd-amdhsa \
+// RUN:   | FileCheck -check-prefix=DEV %s
+// RUN: %clang_cc1 -emit-llvm -o - -fms-extensions %s -triple \
+// RUN:x86_64-pc-windows-msvc | FileCheck -check-prefix=HOST %s
+
+// DEV-NOT: llvm.linker.options
+// DEV-NOT: llvm.dependent-libraries
+// HOST: lvm.linker.options
+// HOST: "/DEFAULTLIB:libcpmt.lib"
+// HOST: "/FAILIFMISMATCH:\22myLib_version=9\22"
+
+#pragma comment(lib, "libcpmt")
+#pragma detect_mismatch("myLib_version", "9")
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -378,15 +378,20 @@
 CmdArgs.push_back("-fexceptions");
 }
 
-static bool ShouldDisableAutolink(const ArgList , const ToolChain ) {
+static bool ShouldEnableAutolink(const ArgList , const ToolChain ,
+ const JobAction ) {
   bool Default = true;
   if (TC.getTriple().isOSDarwin()) {
 // The native darwin assembler doesn't support the linker_option 
directives,
 // so we disable them if we think the .s file will be passed to it.
 Default = TC.useIntegratedAs();
   }
-  return !Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
-   Default);
+  // The linker_option directives are intended for host compilation.
+  if (JA.isDeviceOffloading(Action::OFK_Cuda) ||
+  JA.isDeviceOffloading(Action::OFK_HIP))
+Default = false;
+  return Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
+  Default);
 }
 
 static bool ShouldDisableDwarfDirectory(const ArgList ,
@@ -4391,7 +4396,7 @@
   if (ShouldDisableDwarfDirectory(Args, TC))
 CmdArgs.push_back("-fno-dwarf-directory-asm");
 
-  if (ShouldDisableAutolink(Args, TC))
+  if (!ShouldEnableAutolink(Args, TC, JA))
 CmdArgs.push_back("-fno-autolink");
 
   // Add in -fdebug-compilation-dir if necessary.


Index: clang/test/Driver/hip-autolink.hip
===
--- /dev/null
+++ clang/test/Driver/hip-autolink.hip
@@ -0,0 +1,14 @@
+// REQUIRES: clang-driver
+// REQUIRES: x86-registered-target
+// REQUIRES: amdgpu-registered-target
+//
+// RUN: %clang --target=i386-pc-windows-msvc --cuda-gpu-arch=gfx906 -nogpulib \
+// RUN:   --cuda-device-only -x hip %s -### 2>&1 | FileCheck --check-prefix=DEV %s
+// RUN: %clang --target=i386-pc-windows-msvc --cuda-gpu-arch=gfx906 -nogpulib \
+// RUN:   --cuda-host-only -x hip %s -### 2>&1 | FileCheck --check-prefix=HOST %s
+
+// DEV: "-cc1" "-triple" "amdgcn-amd-amdhsa"
+// DEV-SAME: "-fno-autolink"
+
+// HOST: "-cc1" "-triple" "i386-pc-windows-msvc{{.*}}"
+// HOST-NOT: "-fno-autolink"
Index: clang/test/CodeGenCUDA/ms-linker-options.cu
===
--- /dev/null
+++ clang/test/CodeGenCUDA/ms-linker-options.cu
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -emit-llvm -o - -fcuda-is-device -fms-extensions -x 

[PATCH] D57829: [CUDA][HIP] Disable emitting llvm.linker.options in device compilation

2019-11-04 Thread Yaxun Liu via Phabricator via cfe-commits
yaxunl marked an inline comment as done.
yaxunl added inline comments.



Comment at: clang/test/CodeGenCUDA/linker-options.cu:1-5
+// RUN: %clang_cc1 -emit-llvm -o - -fcuda-is-device -fms-extensions -x hip %s \
+// RUN:   -fno-autolink -triple amdgcn-amd-amdhsa \
+// RUN:   | FileCheck -check-prefix=DEV %s
+// RUN: %clang_cc1 -emit-llvm -o - -fms-extensions -x hip %s -triple \
+// RUN:x86_64-pc-windows-msvc | FileCheck -check-prefix=HOST %s

tra wrote:
> This appears to be specific to HIP on windows. If that's intended, then the 
> file should be renamed to something like `hip-ms-linker-options.cu`. If the 
> changes in functionality apply on other platforms, or with CUDA, the file 
> name can remain as is, but it would be great to add some test runs for the 
> other uses cases.
the change in functionality applies to HIP and CUDA on windows only. will 
rename it to ms-linker-options.cu and add test for CUDA when committing.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D57829/new/

https://reviews.llvm.org/D57829



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D57829: [CUDA][HIP] Disable emitting llvm.linker.options in device compilation

2019-11-04 Thread Artem Belevich via Phabricator via cfe-commits
tra accepted this revision.
tra added a comment.
This revision is now accepted and ready to land.

LGTM in general. Nit about test name and test scope.




Comment at: clang/test/CodeGenCUDA/linker-options.cu:1-5
+// RUN: %clang_cc1 -emit-llvm -o - -fcuda-is-device -fms-extensions -x hip %s \
+// RUN:   -fno-autolink -triple amdgcn-amd-amdhsa \
+// RUN:   | FileCheck -check-prefix=DEV %s
+// RUN: %clang_cc1 -emit-llvm -o - -fms-extensions -x hip %s -triple \
+// RUN:x86_64-pc-windows-msvc | FileCheck -check-prefix=HOST %s

This appears to be specific to HIP on windows. If that's intended, then the 
file should be renamed to something like `hip-ms-linker-options.cu`. If the 
changes in functionality apply on other platforms, or with CUDA, the file name 
can remain as is, but it would be great to add some test runs for the other 
uses cases.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D57829/new/

https://reviews.llvm.org/D57829



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D57829: [CUDA][HIP] Disable emitting llvm.linker.options in device compilation

2019-11-03 Thread Yaxun Liu via Phabricator via cfe-commits
yaxunl updated this revision to Diff 227636.
yaxunl added a comment.

revised by Artem's comments. Also fix test since linker option only supported 
on windows.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D57829/new/

https://reviews.llvm.org/D57829

Files:
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/CodeGenCUDA/linker-options.cu
  clang/test/Driver/hip-autolink.hip


Index: clang/test/Driver/hip-autolink.hip
===
--- /dev/null
+++ clang/test/Driver/hip-autolink.hip
@@ -0,0 +1,14 @@
+// REQUIRES: clang-driver
+// REQUIRES: x86-registered-target
+// REQUIRES: amdgpu-registered-target
+//
+// RUN: %clang --target=i386-pc-windows-msvc --cuda-gpu-arch=gfx906 -nogpulib \
+// RUN:   --cuda-device-only -x hip %s -### 2>&1 | FileCheck 
--check-prefix=DEV %s
+// RUN: %clang --target=i386-pc-windows-msvc --cuda-gpu-arch=gfx906 -nogpulib \
+// RUN:   --cuda-host-only -x hip %s -### 2>&1 | FileCheck --check-prefix=HOST 
%s
+
+// DEV: "-cc1" "-triple" "amdgcn-amd-amdhsa"
+// DEV-SAME: "-fno-autolink"
+
+// HOST: "-cc1" "-triple" "i386-pc-windows-msvc{{.*}}"
+// HOST-NOT: "-fno-autolink"
Index: clang/test/CodeGenCUDA/linker-options.cu
===
--- /dev/null
+++ clang/test/CodeGenCUDA/linker-options.cu
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -emit-llvm -o - -fcuda-is-device -fms-extensions -x hip %s \
+// RUN:   -fno-autolink -triple amdgcn-amd-amdhsa \
+// RUN:   | FileCheck -check-prefix=DEV %s
+// RUN: %clang_cc1 -emit-llvm -o - -fms-extensions -x hip %s -triple \
+// RUN:x86_64-pc-windows-msvc | FileCheck -check-prefix=HOST %s
+
+// DEV-NOT: llvm.linker.options
+// DEV-NOT: llvm.dependent-libraries
+// HOST: lvm.linker.options
+// HOST: "/DEFAULTLIB:libcpmt.lib"
+// HOST: "/FAILIFMISMATCH:\22myLib_version=9\22"
+
+#pragma comment(lib, "libcpmt")
+#pragma detect_mismatch("myLib_version", "9")
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -378,15 +378,20 @@
 CmdArgs.push_back("-fexceptions");
 }
 
-static bool ShouldDisableAutolink(const ArgList , const ToolChain ) {
+static bool ShouldEnableAutolink(const ArgList , const ToolChain ,
+ const JobAction ) {
   bool Default = true;
   if (TC.getTriple().isOSDarwin()) {
 // The native darwin assembler doesn't support the linker_option 
directives,
 // so we disable them if we think the .s file will be passed to it.
 Default = TC.useIntegratedAs();
   }
-  return !Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
-   Default);
+  // The linker_option directives are intended for host compilation.
+  if (JA.isDeviceOffloading(Action::OFK_Cuda) ||
+  JA.isDeviceOffloading(Action::OFK_HIP))
+Default = false;
+  return Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
+  Default);
 }
 
 static bool ShouldDisableDwarfDirectory(const ArgList ,
@@ -4385,7 +4390,7 @@
   if (ShouldDisableDwarfDirectory(Args, TC))
 CmdArgs.push_back("-fno-dwarf-directory-asm");
 
-  if (ShouldDisableAutolink(Args, TC))
+  if (!ShouldEnableAutolink(Args, TC, JA))
 CmdArgs.push_back("-fno-autolink");
 
   // Add in -fdebug-compilation-dir if necessary.


Index: clang/test/Driver/hip-autolink.hip
===
--- /dev/null
+++ clang/test/Driver/hip-autolink.hip
@@ -0,0 +1,14 @@
+// REQUIRES: clang-driver
+// REQUIRES: x86-registered-target
+// REQUIRES: amdgpu-registered-target
+//
+// RUN: %clang --target=i386-pc-windows-msvc --cuda-gpu-arch=gfx906 -nogpulib \
+// RUN:   --cuda-device-only -x hip %s -### 2>&1 | FileCheck --check-prefix=DEV %s
+// RUN: %clang --target=i386-pc-windows-msvc --cuda-gpu-arch=gfx906 -nogpulib \
+// RUN:   --cuda-host-only -x hip %s -### 2>&1 | FileCheck --check-prefix=HOST %s
+
+// DEV: "-cc1" "-triple" "amdgcn-amd-amdhsa"
+// DEV-SAME: "-fno-autolink"
+
+// HOST: "-cc1" "-triple" "i386-pc-windows-msvc{{.*}}"
+// HOST-NOT: "-fno-autolink"
Index: clang/test/CodeGenCUDA/linker-options.cu
===
--- /dev/null
+++ clang/test/CodeGenCUDA/linker-options.cu
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -emit-llvm -o - -fcuda-is-device -fms-extensions -x hip %s \
+// RUN:   -fno-autolink -triple amdgcn-amd-amdhsa \
+// RUN:   | FileCheck -check-prefix=DEV %s
+// RUN: %clang_cc1 -emit-llvm -o - -fms-extensions -x hip %s -triple \
+// RUN:x86_64-pc-windows-msvc | FileCheck -check-prefix=HOST %s
+
+// DEV-NOT: llvm.linker.options
+// DEV-NOT: llvm.dependent-libraries
+// HOST: lvm.linker.options
+// HOST: "/DEFAULTLIB:libcpmt.lib"
+// HOST: "/FAILIFMISMATCH:\22myLib_version=9\22"
+
+#pragma comment(lib, "libcpmt")
+#pragma detect_mismatch("myLib_version", 

[PATCH] D57829: [CUDA][HIP] Disable emitting llvm.linker.options in device compilation

2019-11-01 Thread Yaxun Liu via Phabricator via cfe-commits
yaxunl marked an inline comment as done.
yaxunl added inline comments.



Comment at: clang/lib/Driver/ToolChains/Clang.cpp:389-392
+  // The linker_option directives are intended for host compilation.
+  if (JA.isDeviceOffloading(Action::OFK_Cuda) ||
+  JA.isDeviceOffloading(Action::OFK_HIP))
+Default = false;

tra wrote:
> yaxunl wrote:
> > tra wrote:
> > > Shouldn't it be `true`, considering that we do want to **disable** 
> > > autolinking by default for device-side CUDA/HIP?
> > > 
> > > If we don't support autolinking at all for CUDA/HIP, perhaps we should 
> > > just return `true` here.
> > This variable Default is to be used as the default value of OPT_fautolink. 
> > For device compilation we want the default value to be false. However if 
> > users want to force it to be true, we may still want to respect it.
> You are correct. I've missed the `!` in the return below.  Don't never use 
> double negation. :-/
> 
> Nit: Perhaps we can rename `Default` -> `AutolinkEnabledByDefault` because 
> right now it's easy to misinterpret it as the default return value of the 
> function. Maybe, even change the function itself to `ShouldEnableAutolink()`. 
> 
will change it to ShouldEnableAutolink


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D57829/new/

https://reviews.llvm.org/D57829



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D57829: [CUDA][HIP] Disable emitting llvm.linker.options in device compilation

2019-11-01 Thread Artem Belevich via Phabricator via cfe-commits
tra added inline comments.



Comment at: clang/lib/Driver/ToolChains/Clang.cpp:389-392
+  // The linker_option directives are intended for host compilation.
+  if (JA.isDeviceOffloading(Action::OFK_Cuda) ||
+  JA.isDeviceOffloading(Action::OFK_HIP))
+Default = false;

yaxunl wrote:
> tra wrote:
> > Shouldn't it be `true`, considering that we do want to **disable** 
> > autolinking by default for device-side CUDA/HIP?
> > 
> > If we don't support autolinking at all for CUDA/HIP, perhaps we should just 
> > return `true` here.
> This variable Default is to be used as the default value of OPT_fautolink. 
> For device compilation we want the default value to be false. However if 
> users want to force it to be true, we may still want to respect it.
You are correct. I've missed the `!` in the return below.  Don't never use 
double negation. :-/

Nit: Perhaps we can rename `Default` -> `AutolinkEnabledByDefault` because 
right now it's easy to misinterpret it as the default return value of the 
function. Maybe, even change the function itself to `ShouldEnableAutolink()`. 



CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D57829/new/

https://reviews.llvm.org/D57829



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D57829: [CUDA][HIP] Disable emitting llvm.linker.options in device compilation

2019-10-31 Thread Yaxun Liu via Phabricator via cfe-commits
yaxunl marked an inline comment as done.
yaxunl added inline comments.



Comment at: clang/lib/Driver/ToolChains/Clang.cpp:389-392
+  // The linker_option directives are intended for host compilation.
+  if (JA.isDeviceOffloading(Action::OFK_Cuda) ||
+  JA.isDeviceOffloading(Action::OFK_HIP))
+Default = false;

tra wrote:
> Shouldn't it be `true`, considering that we do want to **disable** 
> autolinking by default for device-side CUDA/HIP?
> 
> If we don't support autolinking at all for CUDA/HIP, perhaps we should just 
> return `true` here.
This variable Default is to be used as the default value of OPT_fautolink. For 
device compilation we want the default value to be false. However if users want 
to force it to be true, we may still want to respect it.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D57829/new/

https://reviews.llvm.org/D57829



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D57829: [CUDA][HIP] Disable emitting llvm.linker.options in device compilation

2019-10-31 Thread Artem Belevich via Phabricator via cfe-commits
tra added inline comments.



Comment at: clang/lib/Driver/ToolChains/Clang.cpp:389-392
+  // The linker_option directives are intended for host compilation.
+  if (JA.isDeviceOffloading(Action::OFK_Cuda) ||
+  JA.isDeviceOffloading(Action::OFK_HIP))
+Default = false;

Shouldn't it be `true`, considering that we do want to **disable** autolinking 
by default for device-side CUDA/HIP?

If we don't support autolinking at all for CUDA/HIP, perhaps we should just 
return `true` here.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D57829/new/

https://reviews.llvm.org/D57829



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D57829: [CUDA][HIP] Disable emitting llvm.linker.options in device compilation

2019-10-31 Thread Yaxun Liu via Phabricator via cfe-commits
yaxunl added a comment.

In D57829#1729094 , @tra wrote:

> Could you, please, give us a bit more context and provide more info for the 
> questions @rjmccall and I asked before?
>
> Is the problem specifically because autolink is not supported on device side? 
> Or is disabling autolink just a convoluted way to avoid calling 
> EmitModuleLinkOptions()?
>  If it's the former, then we should just disable it unconditionally -- we 
> already filter out some other flags  (e.g. asan).
>  If it's the latter, then tweaking autolink option behavior is just covering 
> the problem. Sooner or later EmitModuleLinkOptions() will be called by 
> something else and we'll be back to where we are now.


we need to disable autolink for device compilation because the linker options 
are intended for host compilation.

Previously I said the backend does not support linker option, which was 
incorrect. The diagnostic about invalid linker option was due to a clang 
codegen bug due to detect_mismatch, which I have a fix

https://reviews.llvm.org/D69678

Even with that fix, we still need to disable auto link for device compilation, 
since it is intended for host compilation.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D57829/new/

https://reviews.llvm.org/D57829



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D57829: [CUDA][HIP] Disable emitting llvm.linker.options in device compilation

2019-10-31 Thread Artem Belevich via Phabricator via cfe-commits
tra added a comment.

Could you, please, give us a bit more context and provide more info for the 
questions @rjmccall and I asked before?

Is the problem specifically because autolink is not supported on device side? 
Or is disabling autolink just a convoluted way to avoid calling 
EmitModuleLinkOptions()?
If it's the former, then we should just disable it unconditionally -- we 
already filter out some other flags  (e.g. asan).
If it's the latter, then tweaking autolink option behavior is just covering the 
problem. Sooner or later EmitModuleLinkOptions() will be called by something 
else and we'll be back to where we are now.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D57829/new/

https://reviews.llvm.org/D57829



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D57829: [CUDA][HIP] Disable emitting llvm.linker.options in device compilation

2019-10-30 Thread Yaxun Liu via Phabricator via cfe-commits
yaxunl updated this revision to Diff 227216.
yaxunl retitled this revision from "[HIP] Disable emitting llvm.linker.options 
in device compilation" to "[CUDA][HIP] Disable emitting llvm.linker.options in 
device compilation".
yaxunl edited the summary of this revision.
yaxunl added a comment.

use -fno-autolink to disable linker option for device compilation.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D57829/new/

https://reviews.llvm.org/D57829

Files:
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/CodeGenCUDA/linker-options.cu
  clang/test/Driver/hip-autolink.hip


Index: clang/test/Driver/hip-autolink.hip
===
--- /dev/null
+++ clang/test/Driver/hip-autolink.hip
@@ -0,0 +1,14 @@
+// REQUIRES: clang-driver
+// REQUIRES: x86-registered-target
+// REQUIRES: amdgpu-registered-target
+//
+// RUN: %clang --target=i386-pc-windows-msvc --cuda-gpu-arch=gfx906 -nogpulib \
+// RUN:   --cuda-device-only -x hip %s -### 2>&1 | FileCheck 
--check-prefix=DEV %s
+// RUN: %clang --target=i386-pc-windows-msvc --cuda-gpu-arch=gfx906 -nogpulib \
+// RUN:   --cuda-host-only -x hip %s -### 2>&1 | FileCheck --check-prefix=HOST 
%s
+
+// DEV: "-cc1" "-triple" "amdgcn-amd-amdhsa"
+// DEV-SAME: "-fno-autolink"
+
+// HOST: "-cc1" "-triple" "i386-pc-windows-msvc{{.*}}"
+// HOST-NOT: "-fno-autolink"
Index: clang/test/CodeGenCUDA/linker-options.cu
===
--- /dev/null
+++ clang/test/CodeGenCUDA/linker-options.cu
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -emit-llvm -o - -fcuda-is-device -fms-extensions -x hip %s \
+// RUN:   -fno-autolink | FileCheck -check-prefix=DEV %s
+// RUN: %clang_cc1 -emit-llvm -o - -fms-extensions -x hip %s \
+// RUN:   | FileCheck -check-prefix=HOST %s
+
+// DEV-NOT: llvm.linker.options
+// DEV-NOT: llvm.dependent-libraries
+// HOST: llvm.linker.options
+// HOST: llvm.dependent-libraries
+#pragma comment(lib, "libcpmt")
+#pragma detect_mismatch("myLib_version", "9")
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -378,13 +378,18 @@
 CmdArgs.push_back("-fexceptions");
 }
 
-static bool ShouldDisableAutolink(const ArgList , const ToolChain ) {
+static bool ShouldDisableAutolink(const ArgList , const ToolChain ,
+  const JobAction ) {
   bool Default = true;
   if (TC.getTriple().isOSDarwin()) {
 // The native darwin assembler doesn't support the linker_option 
directives,
 // so we disable them if we think the .s file will be passed to it.
 Default = TC.useIntegratedAs();
   }
+  // The linker_option directives are intended for host compilation.
+  if (JA.isDeviceOffloading(Action::OFK_Cuda) ||
+  JA.isDeviceOffloading(Action::OFK_HIP))
+Default = false;
   return !Args.hasFlag(options::OPT_fautolink, options::OPT_fno_autolink,
Default);
 }
@@ -4362,7 +4367,7 @@
   if (ShouldDisableDwarfDirectory(Args, TC))
 CmdArgs.push_back("-fno-dwarf-directory-asm");
 
-  if (ShouldDisableAutolink(Args, TC))
+  if (ShouldDisableAutolink(Args, TC, JA))
 CmdArgs.push_back("-fno-autolink");
 
   // Add in -fdebug-compilation-dir if necessary.


Index: clang/test/Driver/hip-autolink.hip
===
--- /dev/null
+++ clang/test/Driver/hip-autolink.hip
@@ -0,0 +1,14 @@
+// REQUIRES: clang-driver
+// REQUIRES: x86-registered-target
+// REQUIRES: amdgpu-registered-target
+//
+// RUN: %clang --target=i386-pc-windows-msvc --cuda-gpu-arch=gfx906 -nogpulib \
+// RUN:   --cuda-device-only -x hip %s -### 2>&1 | FileCheck --check-prefix=DEV %s
+// RUN: %clang --target=i386-pc-windows-msvc --cuda-gpu-arch=gfx906 -nogpulib \
+// RUN:   --cuda-host-only -x hip %s -### 2>&1 | FileCheck --check-prefix=HOST %s
+
+// DEV: "-cc1" "-triple" "amdgcn-amd-amdhsa"
+// DEV-SAME: "-fno-autolink"
+
+// HOST: "-cc1" "-triple" "i386-pc-windows-msvc{{.*}}"
+// HOST-NOT: "-fno-autolink"
Index: clang/test/CodeGenCUDA/linker-options.cu
===
--- /dev/null
+++ clang/test/CodeGenCUDA/linker-options.cu
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -emit-llvm -o - -fcuda-is-device -fms-extensions -x hip %s \
+// RUN:   -fno-autolink | FileCheck -check-prefix=DEV %s
+// RUN: %clang_cc1 -emit-llvm -o - -fms-extensions -x hip %s \
+// RUN:   | FileCheck -check-prefix=HOST %s
+
+// DEV-NOT: llvm.linker.options
+// DEV-NOT: llvm.dependent-libraries
+// HOST: llvm.linker.options
+// HOST: llvm.dependent-libraries
+#pragma comment(lib, "libcpmt")
+#pragma detect_mismatch("myLib_version", "9")
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@