Skip to content

Commit

Permalink
Use LLVM to detect CPU features by default if --features aren't speci…
Browse files Browse the repository at this point in the history
…fied.
  • Loading branch information
jemc committed Feb 14, 2017
1 parent ce25599 commit ca17f03
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/libponyc/codegen/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ bool codegen_init(pass_opt_t* opt)
if(opt->features != NULL)
opt->features = LLVMCreateMessage(opt->features);
else
opt->features = LLVMCreateMessage("");
opt->features = LLVMGetHostCPUFeatures();

return true;
}
Expand Down
1 change: 1 addition & 0 deletions src/libponyc/codegen/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ PONY_EXTERN_C_BEGIN

// Missing from C API.
char* LLVMGetHostCPUName();
char* LLVMGetHostCPUFeatures();
void LLVMSetUnsafeAlgebra(LLVMValueRef inst);
void LLVMSetNoUnsignedWrap(LLVMValueRef inst);
void LLVMSetNoSignedWrap(LLVMValueRef inst);
Expand Down
31 changes: 31 additions & 0 deletions src/libponyc/codegen/host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,37 @@ char* LLVMGetHostCPUName()
return strdup(sys::getHostCPUName().str().c_str());
}

char* LLVMGetHostCPUFeatures()
{
StringMap<bool> features;
assert(sys::getHostCPUFeatures(features));

// Calculate the size of buffer that will be needed to return all features.
size_t buf_size = 0;
for(auto it = features.begin(); it != features.end(); it++)
buf_size += (*it).getKey().str().length() + 2; // plus +/- char and ,/null

char* buf = (char*)malloc(buf_size);
assert(buf != NULL);
buf[0] = 0;

for(auto it = features.begin(); it != features.end();)
{
if((*it).getValue())
strcat(buf, "+");
else
strcat(buf, "-");

strcat(buf, (*it).getKey().str().c_str());

it++;
if(it != features.end())
strcat(buf, ",");
}

return buf;
}

void LLVMSetUnsafeAlgebra(LLVMValueRef inst)
{
unwrap<Instruction>(inst)->setHasUnsafeAlgebra(true);
Expand Down
1 change: 1 addition & 0 deletions src/ponyc/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ static void usage()
" =name Default is the host CPU.\n"
" --features CPU features to enable or disable.\n"
" =+this,-that Use + to enable, - to disable.\n"
" Defaults to detecting all CPU features from the host.\n"
" --triple Set the target triple.\n"
" =name Defaults to the host triple.\n"
" --stats Print some compiler stats.\n"
Expand Down

0 comments on commit ca17f03

Please sign in to comment.