#line preprocessor directive cause nvcc to output invalid dependency list

Example:

test.cu:

#line 1 "nonexist_file"

The output of nvcc -M test.cu contains “nonexist_file”. gcc will not output this.

This issue causes C++ extensions that use PyTorch to always rebuild, because PyTorch uses this #line preprocessor directive (e.g. pytorch/THGenerateByteType.h at master · pytorch/pytorch · GitHub). And PyTorch relies on ninja to determine if rebuilding is needed. But ninja cannot find the dependent file, so it decided to rebuild every time. This makes our program slow because torch.utils.cpp_extension.load function will trigger a rebuild on every invocation.

Could you please file a ticket to us following the instruction here Getting Help with CUDA NVCC Compiler so that we can keep you updated of the investigation status.

After checking on our local , we found NVCC takes the original source file, scans it for any presence of “#line 1” and records the entry. What’s going on is the preprocessor adds these lines directives for us. The line 1 is special because it represents the beginning of a file .We treat it specially compared to other numbers.
You can workaround this by picking up any umber but one . For your example

test.cu: 

#line 2 “nonexist_file”

We will make this clearer documented in future releases about this restriction . Hope this helps .