Semiconductor stocks have soundly outperformed the market for years, but Morgan Stanley is now warning the red-hot sector has topped out.

The firm lowered its rating to cautious from in line for the semiconductor industry, citing rising chip inventory levels. The cautious ranking is Morgan Stanley's lowest rating and means its analyst believes the sector will underperform the market over the next 12 to 18 months.

"The semiconductor cycle is showing signs of overheating. … Cyclical indicators are flashing red and any contraction in lead times and/or demand slowdown could lead to a significant inventory correction," analyst Joseph Moore said in a note to clients Thursday. "Furthermore, elevated inventory and stretched lead times leave no margin for error as any lead time adjustment or demand slowdown could drive a meaningful correction. Risk/reward is the poorest it has been in 3 years."

The iShares PHLX Semiconductor ETF is up 12 percent year to date through Wednesday compared with the S&P 500's 7 percent return. Moore noted the PHLX Semiconductor Sector Index is up about 200 percent over the last five years compared with the market's nearly 70 percent gain.

The analyst said inventory levels at chip distributors are at a 10-year high.

As a result, Morgan Stanley's median earnings per share estimates for chip stocks are 2 percent below the Wall Street average for the second half of this year and 4 percent below the consensus for 2019.

"Given the risks we see to semiconductor companies from an overheated semi cycle, we have been conservative in our estimates for broad-based companies," Moore said.

In terms of individual stocks, the firm is lowering its rating to equal weight from overweight for Applied Materials. Morgan Stanley also reduced its rating to underweight from equal weight for On Semiconductor.

— CNBC's contributed to this story.