Skip to content

Commit 5196fe7

Browse files
committedMar 19, 2015
Ignore device-side asm constraint errors while compiling CUDA code for host and vice versa.
Differential Revision: http://reviews.llvm.org/D8392 llvm-svn: 232747
1 parent 152b936 commit 5196fe7

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed
 

Diff for: ‎clang/lib/Sema/SemaStmtAsm.cpp

+16-3
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple,
124124
// The parser verifies that there is a string literal here.
125125
assert(AsmString->isAscii());
126126

127+
bool ValidateConstraints = true;
128+
if (getLangOpts().CUDA) {
129+
// In CUDA mode don't verify asm constraints in device functions during host
130+
// compilation and vice versa.
131+
bool InDeviceMode = getLangOpts().CUDAIsDevice;
132+
FunctionDecl *FD = getCurFunctionDecl();
133+
bool IsDeviceFunction =
134+
FD && (FD->hasAttr<CUDADeviceAttr>() || FD->hasAttr<CUDAGlobalAttr>());
135+
ValidateConstraints = IsDeviceFunction == InDeviceMode;
136+
}
137+
127138
for (unsigned i = 0; i != NumOutputs; i++) {
128139
StringLiteral *Literal = Constraints[i];
129140
assert(Literal->isAscii());
@@ -133,7 +144,8 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple,
133144
OutputName = Names[i]->getName();
134145

135146
TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName);
136-
if (!Context.getTargetInfo().validateOutputConstraint(Info))
147+
if (ValidateConstraints &&
148+
!Context.getTargetInfo().validateOutputConstraint(Info))
137149
return StmtError(Diag(Literal->getLocStart(),
138150
diag::err_asm_invalid_output_constraint)
139151
<< Info.getConstraintStr());
@@ -207,8 +219,9 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple,
207219
InputName = Names[i]->getName();
208220

209221
TargetInfo::ConstraintInfo Info(Literal->getString(), InputName);
210-
if (!Context.getTargetInfo().validateInputConstraint(OutputConstraintInfos.data(),
211-
NumOutputs, Info)) {
222+
if (ValidateConstraints &&
223+
!Context.getTargetInfo().validateInputConstraint(
224+
OutputConstraintInfos.data(), NumOutputs, Info)) {
212225
return StmtError(Diag(Literal->getLocStart(),
213226
diag::err_asm_invalid_input_constraint)
214227
<< Info.getConstraintStr());

Diff for: ‎clang/test/SemaCUDA/asm-constraints-device.cu

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Verify that we do check for constraints in device-side inline
2+
// assembly. Passing an illegal input/output constraint and look
3+
// for corresponding error
4+
// RUN: %clang_cc1 -triple nvptx-unknown-cuda -fsyntax-only -fcuda-is-device -verify %s
5+
6+
__attribute__((device)) void df() {
7+
short h;
8+
int a;
9+
// asm with PTX constraints. Some of them are PTX-specific.
10+
__asm__("output constraints"
11+
: "=h"(h), // .u16 reg, OK
12+
"=a"(a) // expected-error {{invalid output constraint '=a' in asm}}
13+
: // None
14+
);
15+
__asm__("input constraints"
16+
: // None
17+
: "f"(0.0), // .f32 reg, OK
18+
"d"(0.0), // .f64 reg, OK
19+
"h"(0), // .u16 reg, OK
20+
"r"(0), // .u32 reg, OK
21+
"l"(0), // .u64 reg, OK
22+
"a"(0) // expected-error {{invalid input constraint 'a' in asm}}
23+
);
24+
}

Diff for: ‎clang/test/SemaCUDA/asm-constraints-mixed.cu

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang_cc1 -triple nvptx-unknown-cuda -fsyntax-only -fcuda-is-device -verify %s
2+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fsyntax-only -verify %s
3+
// expected-no-diagnostics
4+
5+
__attribute__((device)) void df() {
6+
short h;
7+
// asm with PTX constraints. Some of them are PTX-specific.
8+
__asm__("dont care" : "=h"(h): "f"(0.0), "d"(0.0), "h"(0), "r"(0), "l"(0));
9+
}
10+
11+
void hf() {
12+
int a;
13+
// Asm with x86 constraints that are not supported by PTX.
14+
__asm__("dont care" : "=a"(a): "a"(0), "b"(0), "c"(0));
15+
}

0 commit comments

Comments
 (0)
Please sign in to comment.